File tree 1 file changed +58
-0
lines changed
1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ class MyQueue {
2
+ public:
3
+ stack<int>s1,s2;
4
+ MyQueue() {
5
+
6
+ }
7
+
8
+ void push(int x) {
9
+ s1.push(x);
10
+
11
+ }
12
+
13
+ int pop() {
14
+ int pop = -1;
15
+ if(!s2.empty()){
16
+ pop = s2.top();
17
+ }
18
+ else{
19
+ while(!s1.empty()){
20
+ s2.push(s1.top());
21
+ s1.pop();
22
+ }
23
+ pop = s2.top();
24
+ }
25
+ s2.pop();
26
+ return pop;
27
+ }
28
+
29
+ int peek() {
30
+ int front = -1;
31
+ if(!s2.empty()){
32
+ front = s2.top();
33
+ }
34
+ else{
35
+ while(!s1.empty()){
36
+ s2.push(s1.top());
37
+ s1.pop();
38
+ }
39
+ front = s2.top();
40
+ }
41
+
42
+ return front;
43
+ }
44
+
45
+ bool empty() {
46
+ return s1.empty() && s2.empty();
47
+ }
48
+
49
+ };
50
+
51
+ /**
52
+ * Your MyQueue object will be instantiated and called as such:
53
+ * MyQueue* obj = new MyQueue();
54
+ * obj->push(x);
55
+ * int param_2 = obj->pop();
56
+ * int param_3 = obj->peek();
57
+ * bool param_4 = obj->empty();
58
+ */
You can’t perform that action at this time.
0 commit comments