Implement Stack using Queues LeetCode solution in C++

1
class MyStack {
2
public:
3
/** Initialize your data structure here. */
4
queue<int> q1;
5
MyStack() {}
6

7
/** Push element x onto stack. */
8
void push(int x) {
9
int sz = q1.size();
10
q1.push(x);
11
while (sz--) {
12
q1.push(q1.front());
13
q1.pop();
14
}
15
}
16

17
/** Removes the element on top of the stack and returns that element. */
18
int pop() {
19
int result = top();
20
q1.pop();
21
return result;
22
}
23

24
/** Get the top element. */
25
int top() {
26
return q1.front();
27
}
28

29
/** Returns whether the stack is empty. */
30
bool empty() {
31
return q1.empty();
32
}
33
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0