Stack using two queues | Practice | GeeksforGeeks

Implement a Stack using two queues q1 and q2.
Example 1:
Input:
push(2)
push(3)
pop()
push(4)
pop()
Output: 3 4
Explanation:
push(2) the stack will be {2}
push(3) the stack will be {2 3}
pop()   poped element will be 3 the 
        stack will be {2}
push(4) the stack will be {2 4}
pop()   poped element will be 4  

Example 2:
Input:
push(2)
pop()
pop()
push(3)
Output: 2 -1
Your Task:
Since this is a function problem, you don't need to take inputs. You are required to complete the two methods push() which takes an integer 'x' as input denoting the element to be pushed into the stack and pop() which returns the integer poped out from the stack(-1 if the stack is empty).
Expected Time Complexity: O(1) for push() and O(N) for pop() (or vice-versa).
Expected Auxiliary Space: O(1) for both push() and pop().
Constraints:
1 <= Number of queries <= 100
1 <= values of the stack <= 100
-1
smitasinghthakur1891 week ago
void QueueStack :: push(int x)
{
    q1.push(x);   
}

//Function to pop an element from stack using two queues. 
int QueueStack :: pop()
{
    if(q1.empty()) return -1;
    while(q1.size()>1)
    {
        q2.push(q1.front());
        q1.pop();
    }
    int x=q1.front();
    q1.pop();
    while(!q2.empty())
    {
        q1.push(q2.front());
        q2.pop();
    }
    return x;      
}
0
shaikhmohammedammar1 week ago

//Function to push an element into stack using two queues.
void QueueStack :: push(int x){
    // if both are empty, then it's the first element, start with q1
   if(q1.empty() && q2.empty()) q1.push(x);
//   if q2 or q1 is not empty, then it's in the process, push in it.
   else if(q1.empty() && !q2.empty()) q2.push(x);
   else q1.push(x);
}

int QueueStack :: pop()
{
    // IF Q1 is not empty, then it's in process, empty it in Q2, return the last element.
       if(!q1.empty()){
           while(q1.size() != 1){
               q2.push(q1.front());
               q1.pop();
           }
           int x = q1.front(); q1.pop();
           return x;
       }
    //   else if Q2 is not empty, empty it in Q1, return the last element.
       else if(!q2.empty()){
           while(q2.size() != 1){
               q1.push(q2.front());
               q2.pop();
           }
           int x = q2.front(); q2.pop();
           return x;
       }
    //   else both are empty, return nothing, ie.-1.
       return -1;
     
}
0
hharshit81182 weeks ago
void QueueStack :: push(int x){   if(q1.empty() && q2.empty()){       q1.push(x);   }   else if(q1.empty() && !q2.empty()){       q2.push(x);   }   else{       q1.push(x);   }}
int QueueStack :: pop(){       if(q1.empty() == false){           while(q1.size() != 1){               q2.push(q1.front());               q1.pop();           }           int x = q1.front(); q1.pop();           return x;       }             else if(q2.empty() == false){           while(q2.size() != 1){               q1.push(q2.front());               q2.pop();           }           int x = q2.front(); q2.pop();           return x;       }       else{           return -1;       }}
0
ankitap99322 weeks ago
//Function to push an element into stack using two queues.void QueueStack :: push(int x){   //step-1 push into q2   //step-2 push remaining elements of q1 into q2   //step-3 swap q1 and q2   q2.push(x);      while(q1.size()!=0){       q2.push(q1.front());       q1.pop();   }   swap(q1,q2);   //push using one queue only approach   //step-1 push into q1   //step-2 rotate q1 elements from front to q1.size()-1 elements        // q1.push(x);       // int rotate=q1.size()-1;       // while(rotate--){       //     q1.push(q1.front());       //     q1.pop();       // }}
//Function to pop an element from stack using two queues. int QueueStack :: pop(){   int ans;   if(q1.empty()){       ans=-1;   }   else{       ans=q1.front();       q1.pop();   }  return ans; }
+1
swastikp17111 month ago
class Queues
{
    Queue<Integer> q1 = new LinkedList<Integer>();
    Queue<Integer> q2 = new LinkedList<Integer>();
    
    //Function to push an element into stack using two queues.
    void push(int a)
    {
	    while(!q1.isEmpty()){
	        q2.offer(q1.poll());
	    }
	    q1.offer(a);
	    while(!q2.isEmpty()){
	        q1.offer(q2.poll());
	    }
    }
    
    //Function to pop an element from stack using two queues. 
    int pop()
    {
	    if(q1.isEmpty()) return -1;
	    return q1.poll();
    }
	
}
+1
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION (0.4/1.7 SEC)
class Queues{   Queue<Integer> q1 = new LinkedList<Integer>();   Queue<Integer> q2 = new LinkedList<Integer>();      //Function to push an element into stack using two queues.   void push(int a)   {    if(q1.isEmpty())    {        q1.add(a);    }    else    {        while(!q1.isEmpty())        {            q2.add(q1.remove());        }        q1.add(a);        while(!q2.isEmpty())        {            q1.add(q2.remove());        }    }   }      //Function to pop an element from stack using two queues.    int pop()   {       int x=-1;    if(!q1.isEmpty())    {      x=q1.remove();    }    return x;   }}
0
mohankumar9875mk1 month ago
JS code:(partial)
 
class QueueStack{   constructor(){       this.q1 = new Queue();         this.q2 = new Queue();    }   push(x){              this.q2.push(x);       while(this.q2.length===x){           this.q1.push(this.q2.pop());       }       (this.q1),(this.q2)=(this.q1),(this.q2)   }   pop(){       return(this.q1.pop())   }}
0
sunboykenneth1 month ago
class StackWithQueues {private:   queue<int> q1;   queue<int> q2;
public:   void push(int data) {       queue<int> *targetQ = NULL;       if (!q1.empty()) {           targetQ = &q1;       } else {           targetQ = &q2;       }              targetQ->push(data);   }      int pop() {       queue<int> *targetQ = NULL, *newQ = NULL;       if (!q1.empty()) {           targetQ = &q1;           newQ = &q2;       } else if (!q2.empty()) {           targetQ = &q2;           newQ = &q1;       } else {           return 0;       }              int size = targetQ->size();       size--;              while(size > 0) {           newQ->push(targetQ->front());           targetQ->pop();           size--;       }              int data = targetQ->front();       targetQ->pop();       return data;   }   };
+1
amanasati11 month ago
Implementing stack using 2 queue push operationStep 1- insert into Q1Step 2- push remaining elements of Q2 into Q1Step 3- Swap Q1 and Q2
 
pop operationpop from Q2 as all the element in Q2
 
Below is easy pythonic approach
 
def push(x):
    
    # global declaration
    global queue_1
    global queue_2
    
    # code here
    queue_1.append(x)
    while queue_2:
        queue_1.append(queue_2.pop(0))
    queue_1,queue_2=queue_2,queue_1


#Function to pop an element from stack using two queues.     
def pop():
    
    # global declaration
    global queue_1
    global queue_2
    # code here
    if not queue_2:
        return -1
        
    return queue_2.pop(0)
+1
detroix072 months ago
void QueueStack :: push(int x){    while(q1.size()!=0)  {    q2.push(q1.front());    q1.pop();  }    q1.push(x);    while(q2.size()!=0)  {    q1.push(q2.front());    q2.pop();  } }
 
//Function to pop an element from stack using two queues.
 int QueueStack :: pop(){   if(q1.size()==0) return -1;    int Ele = q1.front();    q1.pop();    return Ele;} 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.