We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 891f5d9 commit ac71c21Copy full SHA for ac71c21
225 Implement Stack using Queues .cpp
@@ -0,0 +1,32 @@
1
+class Stack {
2
+public:
3
+ // Push element x onto stack.
4
+ void push(int x) {
5
+ while (!q.empty()) {
6
+ tmp.push(q.front());
7
+ q.pop();
8
+ }
9
+ q.push(x);
10
+ while (!tmp.empty()) {
11
+ q.push(tmp.front());
12
+ tmp.pop();
13
14
15
+
16
+ // Removes the element on top of the stack.
17
+ void pop() {
18
19
20
21
+ // Get the top element.
22
+ int top() {
23
+ return q.front();
24
25
26
+ // Return whether the stack is empty.
27
+ bool empty() {
28
+ return q.empty();
29
30
+private:
31
+ queue<int> q, tmp;
32
+};
0 commit comments