Queue using two Stacks | Practice | GeeksforGeeks

Implement a Queue using 2 stacks s1 and s2 .
A Query Q is of 2 Types
(i) 1 x (a query of this type means  pushing 'x' into the queue)
(ii) 2   (a query of this type means to pop element from queue and print the poped element)
Example 1:
Input:
5
1 2 1 3 2 1 4 2

Output: 
2 3

Explanation: 
In the first testcase
1 2 the queue will be {2}
1 3 the queue will be {2 3}
2   poped element will be 2 the queue 
    will be {3}
1 4 the queue will be {3 4}
2   poped element will be 3.

Example 2:
Input:
4
1 2 2 2 1 4

Output: 
2 -1

Explanation: 
In the second testcase 
1 2 the queue will be {2}
2   poped element will be 2 and 
    then the queue will be empty
2   the queue is empty and hence -1
1 4 the queue will be {4}.
Your Task:
You are required to complete the two methods push which take one argument an integer 'x' to be pushed into the queue and pop which returns a integer poped out from other queue(-1 if the queue is empty). The printing is done automatically by the driver code.
Expected Time Complexity : O(1) for push() and O(N) for pop() or O(N) for push() and O(1) for pop()  
Expected Auxilliary Space : O(1).
Constraints:
1 <= Q <= 100
1 <= x <= 100
Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
madhajaswanth2 days ago
class StackQueue{   Stack<Integer> s1 = new Stack<Integer>();   Stack<Integer> s2 = new Stack<Integer>();
   void Push(int x)   {    // if the stack1 is empty then push the item into stack1    // else     //      pop all elements from stack1 and push to stack2    //      push new element into stack 1    //      pop all elements from stack2 and push to stack1        if(s1.isEmpty()){        s1.push(x);        return;    }    while(!s1.isEmpty()){        s2.push(s1.pop());    }    s1.push(x);    while(!s2.isEmpty()){        s1.push(s2.pop());    }   }   int Pop()   {       // pop the top item of stack1      return s1.isEmpty() ? -1 : s1.pop();   }} 
0
harshscode2 weeks ago
push
 
 while(!s1.empty())   {       s2.push(s1.top());       s1.pop();   }   s1.push(x);      while(!s2.empty())   {       s1.push(s2.top());       s2.pop();   }
 
pop
 
  int ans;   if(!s1.empty())   {       ans=s1.top();       s1.pop();   }   else   {     ans=-1;   }   return ans;
0
tarunkanade2 weeks ago
0.37/1.48
Making push costly
void Push(int x)
    {
	   // Your code here
	   while(!s1.isEmpty()){
	       s2.push(s1.pop());
	   }
	   s1.push(x);
	   
	   while(!s2.isEmpty()){
	       s1.push(s2.pop());
	   }
    }
	
    
    //Function to pop an element from queue by using 2 stacks.
    int Pop()
    {
	   // Your code here
	   if(s1.isEmpty()){
	       return -1;
	   }
	   return s1.pop();
    }
0
hharshit81182 weeks ago
void StackQueue :: push(int x){   s1.push(x);}
//Function to pop an element from queue by using 2 stacks.int StackQueue :: pop(){   if(s1.empty() == false){       while(s1.size() != 1){           s2.push(s1.top());           s1.pop();       }       int x = s1.top();       s1.pop();       while(s2.empty() == false){           s1.push(s2.top());           s2.pop();       }       return x;   }   else{       return -1;   }}
0
19bcs13143 weeks ago
class StackQueue
{
    Stack<Integer> s1 = new Stack<Integer>();
    Stack<Integer> s2 = new Stack<Integer>();

    //Function to push an element in queue by using 2 stacks.
    void Push(int x)
    {
	   // Your code here
	   while(s2.size()!=0){
	       s1.push(s2.pop());
	   }
	   s1.push(x);
    }
	
    
    //Function to pop an element from queue by using 2 stacks.
    int Pop()
    {
	   // Your code here
	   while(s1.size()!=0){
	       s2.push(s1.pop());
	   }
	   if(s2.size()!=0) return s2.pop();
	   else return -1;
    }
}

0
indraneelghosh7401 month ago
void StackQueue :: push(int x){   s1.push(x);}int StackQueue :: pop(){   if(s1.empty())   return -1;       while(!s1.empty()){           s2.push(s1.top());           s1.pop();       }       int x=s2.top();       s2.pop();
       while(!s2.empty()){           s1.push(s2.top());           s2.pop();       }       return x;} 
0
ishan1010011 month ago
Can some identify the problem in this solution?
 
void StackQueue :: push(int x){   // Your Code   s2.push(x);   while(!s1.empty()){       s2.push(s1.top());       s1.pop();   }   swap(s1,s2);}
 
int StackQueue :: pop(){       // Your Code           int ans = 0;       if(s1.empty()) return -1;       else{           ans = s1.top();           s1.pop();       }       return ans;}
+1
shreyanshsinha1 month ago
stack <int> s1;stack <int> s2;
void StackQueue :: push(int x){   s1.push(x);   // Your Code}
//Function to pop an element from queue by using 2 stacks.int StackQueue :: pop(){   if(s1.size() == 0)       return -1;          while(!s1.empty())   {       s2.push(s1.top());       s1.pop();   }      int ans = s2.top();   s2.pop();      while(!s2.empty())   {       s1.push(s2.top());       s2.pop();   }      return ans;       // Your Code       }
+1
navidulhaque1 month ago
C++, Push-O(n), Pop-O(1)
//Function to push an element in queue by using 2 stacks.
void StackQueue :: push(int x)
{
    while(!s1.empty()){
        int tmp=s1.top();
        s2.push(tmp);
        s1.pop();
    }
    s1.push(x);
    while(!s2.empty()){
        int tmp=s2.top();
        s1.push(tmp);
        s2.pop();
    }
}

//Function to pop an element from queue by using 2 stacks.
int StackQueue :: pop()
{
        if(s1.empty())return -1;
        int res=s1.top();
        s1.pop();
        return res;
}
0
harsh prakashPremium2 months ago
EASY AND SMALL JAVA  SOLUTION
 
class StackQueue
{
    Stack<Integer> s1 = new Stack<Integer>();
    Stack<Integer> s2 = new Stack<Integer>();
    
    void Push(int x)
    {
	   s1.push(x);
    }

    int Pop()
    {
	   if(s2.isEmpty()) {
	       while(s1.size()>0) {
    	       s2.push(s1.pop());
    	   }
	   }
	   int el = s2.size()>0 ? s2.pop() : -1;
	   return el;
    }
}
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.