You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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.
// 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
+
classMyStack {
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
+
voidpush(int x) {
59
+
if (b.empty()){
60
+
a.push(x);
61
+
q = true;
62
+
}
63
+
elseif (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
+
intpop() {
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
+
inttop() {
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
+
boolempty() {
129
+
return a.empty() && b.empty();
130
+
}
131
+
};
132
+
133
+
/**
134
+
* Your MyStack object will be instantiated and called as such:
0 commit comments