Queue using two Stacks
PROBLEM :
Implement a Queue using 2 stacks s1 and s2 .
Input (To be used for Expected Output):
The first line of the input contains an integer 'T' denoting the number of test cases. Then T test cases follow.
First line of each test case contains an integer Q denoting the number of queries .
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)
The second line of each test case contains Q queries seperated by space.
Output:
The output for each test case will be space separated integers having -1 if the queue is empty else the element poped out from the queue .
You are required to complete the two methods push which take one argument an integer 'x' to be pushed into the quee and pop which returns a integer poped out from othe queue.
Constraints:
1<=T<=100
1<=Q<=100
1<=x<=100
Example:
Input
1
5
1 2 1 3 2 1 4 2
Output
2 3
Explanation:
In the first test case for query
1 2 the quee 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
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* The structure of the class is
class StackQueue{
private:
// These are STL stacks ( http://goo.gl/LxlRZQ )
stack<int> s1;
stack<int> s2;
public:
void push(int);
int pop();
}; */
/* The method push to push element into the queue */
void StackQueue :: push(int x)
{
s1.push(x) ;
}
/*The method pop which return the element poped out of the queue*/
int StackQueue :: pop()
{
if(s1.empty())
return -1 ;
while(!s1.empty())
{
s2.push(s1.top()) ;
s1.pop() ;
}
int temp ;
temp=s2.top();
s2.pop() ;
while(!s2.empty())
{
s1.push(s2.top()) ;
s2.pop() ;
}
return temp ;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Using one stack & recurtion)
--------------------------------------------------------------------------------
credits : https://www.facebook.com/golupatidar.patidar.9
/* The structure of the class is
class StackQueue{
private:
// These are STL stacks ( http://goo.gl/LxlRZQ )
stack<int> s1;
stack<int> s2;
public:
void push(int);
int pop();
}; */
/* The method push to push element into the queue */
/* The method push to push element into the queue */
int Pop();
void StackQueue :: push(int x)
{
s1.push(x) ;
}
/*The method pop which return the element poped out of the queue*/
int StackQueue :: pop()
{
int n;
if(s1.empty())
return -1;
n=s1.top();
if(s1.size()==1)
{
s1.pop();
return n;
}
s1.pop();
int res=pop();
s1.push(n);
return res;
}
---------------------------------------------------------------------------------
Implement a Queue using 2 stacks s1 and s2 .
Input (To be used for Expected Output):
The first line of the input contains an integer 'T' denoting the number of test cases. Then T test cases follow.
First line of each test case contains an integer Q denoting the number of queries .
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)
The second line of each test case contains Q queries seperated by space.
Output:
The output for each test case will be space separated integers having -1 if the queue is empty else the element poped out from the queue .
You are required to complete the two methods push which take one argument an integer 'x' to be pushed into the quee and pop which returns a integer poped out from othe queue.
Constraints:
1<=T<=100
1<=Q<=100
1<=x<=100
Example:
Input
1
5
1 2 1 3 2 1 4 2
Output
2 3
Explanation:
In the first test case for query
1 2 the quee 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
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* The structure of the class is
class StackQueue{
private:
// These are STL stacks ( http://goo.gl/LxlRZQ )
stack<int> s1;
stack<int> s2;
public:
void push(int);
int pop();
}; */
/* The method push to push element into the queue */
void StackQueue :: push(int x)
{
s1.push(x) ;
}
/*The method pop which return the element poped out of the queue*/
int StackQueue :: pop()
{
if(s1.empty())
return -1 ;
while(!s1.empty())
{
s2.push(s1.top()) ;
s1.pop() ;
}
int temp ;
temp=s2.top();
s2.pop() ;
while(!s2.empty())
{
s1.push(s2.top()) ;
s2.pop() ;
}
return temp ;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Using one stack & recurtion)
--------------------------------------------------------------------------------
credits : https://www.facebook.com/golupatidar.patidar.9
/* The structure of the class is
class StackQueue{
private:
// These are STL stacks ( http://goo.gl/LxlRZQ )
stack<int> s1;
stack<int> s2;
public:
void push(int);
int pop();
}; */
/* The method push to push element into the queue */
/* The method push to push element into the queue */
int Pop();
void StackQueue :: push(int x)
{
s1.push(x) ;
}
/*The method pop which return the element poped out of the queue*/
int StackQueue :: pop()
{
int n;
if(s1.empty())
return -1;
n=s1.top();
if(s1.size()==1)
{
s1.pop();
return n;
}
s1.pop();
int res=pop();
s1.push(n);
return res;
}
---------------------------------------------------------------------------------
Comments
Post a Comment