Skip to content

Commit 623916b

Browse files
committed
stacks using queues
1 parent 0cae96e commit 623916b

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// 225. Implement Stack using Queues
2+
3+
// https://leetcode.com/problems/implement-stack-using-queues/
4+
5+
// difficulty: easy
6+
7+
// Implement a last in first out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal queue (push, top, pop, and empty).
8+
9+
// Implement the MyStack class:
10+
11+
// void push(int x) Pushes element x to the top of the stack.
12+
// int pop() Removes the element on the top of the stack and returns it.
13+
// int top() Returns the element on the top of the stack.
14+
// boolean empty() Returns true if the stack is empty, false otherwise.
15+
// Notes:
16+
17+
// You must use only standard operations of a queue, which means only push to back, peek/pop from front, size, and is empty operations are valid.
18+
// Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue), as long as you use only a queue's standard operations.
19+
20+
21+
// Example 1:
22+
23+
// Input
24+
// ["MyStack", "push", "push", "top", "pop", "empty"]
25+
// [[], [1], [2], [], [], []]
26+
// Output
27+
// [null, null, null, 2, 2, false]
28+
29+
// Explanation
30+
// MyStack myStack = new MyStack();
31+
// myStack.push(1);
32+
// myStack.push(2);
33+
// myStack.top(); // return 2
34+
// myStack.pop(); // return 2
35+
// myStack.empty(); // return False
36+
37+
38+
// Constraints:
39+
40+
// 1 <= x <= 9
41+
// At most 100 calls will be made to push, pop, top, and empty.
42+
// All the calls to pop and top are valid.
43+
44+
45+
// Follow-up: Can you implement the stack such that each operation is amortized O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer. You can use more than two queues.
46+
47+
class MyStack {
48+
public:
49+
queue <int> a;
50+
queue <int> b;
51+
bool q = false;
52+
/** Initialize your data structure here. */
53+
MyStack() {
54+
55+
}
56+
57+
/** Push element x onto stack. */
58+
void push(int x) {
59+
if (b.empty()){
60+
a.push(x);
61+
q = true;
62+
}
63+
else if (a.empty()){
64+
b.push(x);
65+
q = false;
66+
}
67+
}
68+
69+
/** Removes the element on top of the stack and returns that element. */
70+
int pop() {
71+
int c,ans;
72+
if (q){
73+
c = a.size()-1;
74+
while (c--){
75+
b.push(a.front());
76+
a.pop();
77+
}
78+
ans = a.front();
79+
a.pop();
80+
q = false;
81+
return ans;
82+
}
83+
else{
84+
c = b.size()-1;
85+
while (c--){
86+
a.push(b.front());
87+
b.pop();
88+
}
89+
ans = b.front();
90+
b.pop();
91+
q = true;
92+
return ans;
93+
}
94+
}
95+
96+
/** Get the top element. */
97+
int top() {
98+
int c,ans;
99+
if (q){
100+
c = a.size()-1;
101+
while (c>0){
102+
b.push(a.front());
103+
a.pop();
104+
c--;
105+
}
106+
ans=a.front();
107+
b.push(a.front());
108+
a.pop();
109+
q = false;
110+
return ans;
111+
}
112+
else{
113+
c = b.size()-1;
114+
while (c>0){
115+
a.push(b.front());
116+
b.pop();
117+
c--;
118+
}
119+
a.push(b.front());
120+
ans = b.front();
121+
b.pop();
122+
q = true;
123+
return ans;
124+
}
125+
}
126+
127+
/** Returns whether the stack is empty. */
128+
bool empty() {
129+
return a.empty() && b.empty();
130+
}
131+
};
132+
133+
/**
134+
* Your MyStack object will be instantiated and called as such:
135+
* MyStack* obj = new MyStack();
136+
* obj->push(x);
137+
* int param_2 = obj->pop();
138+
* int param_3 = obj->top();
139+
* bool param_4 = obj->empty();
140+
*/

0 commit comments

Comments
 (0)