Implementation of Stack Using Two queue

#include<bits/stdc++.h>

using namespace std;

class Stack{
queue q1,q2;
int cs;
public:
Stack(){
this->cs=0;
}

	bool isEmpty(){
		return this->cs==0;
	}
	
	void Push(int data){
			//Push the element in one of the queue
			if(q2.empty()){
				q2.push(data);	
			}
				
		
		//Pop all the elements from one of the queue and push it in the other
		//queue until the queue becomes empty.
		
		while(!q1.empty()){
			q2.push(q1.front());
			q1.pop();		
		}
		
		//Swap the names of the queues
		queue<int> q;
		q=q1;
		q1=q2;
		q2=q;
		
		//Increment the size of the array
		cs++;
	}
	
	void Pop(){
		
		if(q1.empty() && q2.empty()){
			return;
		}
		
		while(q1.size()!=1){
			q2.push(q1.front());
			q1.pop();
		}
		q1.pop();
		
		
		queue<int>q=q1;
		q1=q2;
		q2=q;			
		cs--;
	}
	
	int top(){
		if(q1.empty()){
			return 0;
		}
		return q1.front();
	}
	
	int size(){
		return cs;
	}

};

int main(){
Stack S;

for(int i=1;i<=10;i++){
	S.Push(i);
}

// S.Pop();
// S.Push(11);

while(!S.isEmpty()){
	cout<<S.top()<<" ";
	S.Pop();
}
	
return 0;

}

Unable To get the correct answer

please first tell what you are trying to do exaclty and which is your main queue and which is secondary queue, because your code doesn’t matches with the Title.

Actually I have to implement stack using 2 queues.

My main queue is q1, and the secondary is q2

@Gunit Okay so what you want to make easy operation among push or pop

I think you are trying to make pop operation in constant time.
So for this, in push function,
first put all elements of q1 into q2 first,
then push the current element into q1
then push back again from q2 to q1.

For pop function, it is just the front element

yes,I am trying to make it push as well as pop efficient

I have switched their names…can u pls send me your code so that I could dry run run it to see it where I was wrong

Bro you can’t make both the things efficient, only one thing either push or pop efficient is required to be done here. Wait I am sending you the code

@Gunit https://ide.codingblocks.com/s/206618
I just did the same as i have said above

ok…I got my mistake…actually I was switching the names of the queues which was making a sort of confusion.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.