Problem was to implement stack using two queue.
We need to perform basic operations like push,pop,top,empty.
Primary queue was q1.
for push(),empty() and top() I used only one queue(q1)
push()
void push(int x) {
q1.push(x);
}empty()
bool empty() {
return q1.empty();
}top()
int top() {
return q1.back();
}pop()
int pop() {
queue<int> q2; //creating q1
int x;
while(q1.size()!=1){ // moved everything from q1 to q2 leaving the last element.
x=q1.front();
q1.pop();
q2.push(x);
}
x=q1.front(); //storing and deleting last element from q1
q1.pop();
q1=q2; //coping everything from q2 to q1
return x; //returning x
}
Time complexity:
I hope you will find my solution helpful!😊