28. Implement stack using queues
My Approach
Time and Auxiliary Space Complexity
Code (C++)
class MyStack {
queue<int> q1, q2;
public:
MyStack() {
}
void push(int x) {
q2.push(x);
while(!q1.empty()){
q2.push(q1.front());
q1.pop();
}
queue<int> temp;
temp = q1;
q1 = q2;
q2 = temp;
}
int pop() {
int val = q1.front();
q1.pop();
return val;
}
int top() {
return q1.front();
}
bool empty() {
if(q1.empty()) return true;
else return false;
}
};Contribution and Support
Last updated