Implement two stacks in an array
PROBLEM :
Your task is to implement 2 stacks in one array efficiently .
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 4 Types
(i) 1 1 x (a query of this type means pushing 'x' into the stack 1)
(ii) 1 2 (a query of this type means to pop element from stack1 and print the poped element)
(iii) 2 1 x (a query of this type means poushing 'x' into the stack 2)
(iv) 2 2 (a query of this type means to pop element from stack2 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 stack is empty else the element poped out from the stack.
You are required to complete the 4 methods push1, push2 which takes one argument an integer 'x' to be pushed into the stack one and two and pop1, pop2 which returns a integer poped out from stack one and two .
Constraints:
1<=T<=100
1<=Q<=100
1<=x<=100
Example:
Input
1
6
1 1 2 1 1 3 2 1 4 1 2 2 2 2 2
Output
3 4 -1
Explanation:
In the first test case for query
1 1 2 the stack1 will be {2}
1 1 3 the stack1 will be {2,3}
2 1 4 the stack2 will be {4}
1 2 the poped element will be 3 from stack1 and stack1 will be {2}
2 2 the poped element will be 4 from stack2 and now stack2 is empty
2 2 the stack2 is now empty hence -1 .
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*The structure of the class is
class twoStacks
{
int *arr;
int size;
int top1, top2;
public:
twoStacks(int n=100){size = n; arr = new int[n]; top1 = -1; top2 = size;}
void push1(int x);
void push2(int x);
int pop1();
int pop2();
};
*/
/* The method push to push element into the stack 2 */
void twoStacks :: push1(int x)
{
if(top1>=top2)
return ;
arr[++top1]=x ;
}
/* The method push to push element into the stack 2*/
void twoStacks ::push2(int x)
{
if(top1>=top2)
return ;
arr[--top2]=x ;
}
/* The method pop to pop element from the stack 1 */
int twoStacks ::pop1()
{
if(top1==-1)
return -1 ;
else
return(arr[top1--]) ;
}
/* The method pop to pop element from the stack 2 */
int twoStacks :: pop2()
{
if(top2==size)
return -1 ;
else
return(arr[top2++]) ;
}
--------------------------------------------------------------------------------
COMPLETE STACK IMPLEMENTATION IN ARRAY :
--------------------------------------------------------------------------------
// implementing two stack in a single array
#include<iostream>
using namespace std ;
#include<malloc.h>
typedef struct STACK
{
int top1,top2 ;
int capasity ;
int *array ;
}stack ;
void create_stack(stack **,int );
void push_stack1(stack **,int );
void push_stack2(stack **,int );
int pop_stack1(stack **);
int pop_stack2(stack **);
int isfull_stack(stack *);
int isempty_stack(stack *);
void display_stack1(stack *) ;
void display_stack2(stack *) ;
int main()
{
stack *s ;
int no,choice,i,ele ;
cout<<"\n enter the capacity of the stack " ;
cin>>no ;
create_stack(&s,no) ;
do
{
cout<<"\n 1. push elemet in the first stack " ;
cout<<"\n 2. push elemet in the second stack " ;
cout<<"\n 3. pop element from the first stack " ;
cout<<"\n 4. pop element from the second stack " ;
cout<<"\n 5. display first stack " ;
cout<<"\n 6. display second stack " ;
cout<<"\n enter choice" ;
cin>>choice ;
switch(choice)
{
case 1 :cout<<"\n enter no of elemets to push " ;
cin>>no ;
cout<<"\n enter th elemets " ;
for(i=0;i<no;i++)
{
cin>>ele ;
push_stack1(&s,ele) ;
}
break ;
case 2 :cout<<"\n enter no of elemets to push " ;
cin>>no ;
cout<<"\n enter th elemets " ;
for(i=0;i<no;i++)
{
cin>>ele ;
push_stack2(&s,ele) ;
}
break ;
case 3 :ele=pop_stack1(&s) ;
if(ele!=-1)
cout<<"\n elemet poped is = "<<ele ;
break ;
case 4 :ele=pop_stack1(&s) ;
if(ele!=-1)
cout<<"\n elemet poped is = "<<ele ;
break ;
case 5 :display_stack1(s) ;
break ;
case 6 :display_stack2(s) ;
break ;
default : cout<<"\n TRY AGAIN !!!!" ;
}
cout<<"\n enter 1 to continue " ;
cin>>no ;
}while(no==1) ;
return 0 ;
}
void create_stack(stack **s,int no)
{
(*s)=(stack*)malloc(sizeof(stack)) ;
(*s)->top1=-1;
(*s)->top2=no ;
(*s)->capasity=no ;
(*s)->array=(int*)malloc((*s)->capasity*(sizeof(int))) ;
}
void push_stack1(stack **s,int data)
{
if(isfull_stack(*s))
{
cout<<"\n SORRY !!! can't take more input !!! STACK FULLL" ;
return ;
}
(*s)->array[++((*s)->top1)]=data ;
}
void push_stack2(stack **s,int data)
{
if(isfull_stack(*s))
{
cout<<"\n SORRY !!! can't take more input !!! STACK FULLL" ;
return ;
}
(*s)->array[--((*s)->top2)]=data ;
}
int pop_stack1(stack **s)
{
if(isempty_stack(*s))
{
cout<<"\n SORRY !!! stack is empty !! no elemet to POP" ;
return -1 ;
}
return((*s)->array[((*s)->top1)--]) ;
}
int pop_stack2(stack **s)
{
if(isempty_stack(*s))
{
cout<<"\n SORRY !!! stack is empty !! no elemet to POP" ;
return -1 ;
}
return((*s)->array[((*s)->top2)--]) ;
}
void display_stack1(stack *s)
{
if(isempty_stack(s))
{
cout<<"\n SORRY !!! stack is empty !! no elemet to display" ;
return ;
}
int i ;
for(i=0;i<=s->top1;i++)
cout<<s->array[i]<<" " ;
}
void display_stack2(stack *s)
{
if(isempty_stack(s))
{
cout<<"\n SORRY !!! stack is empty !! no elemet to display" ;
return ;
}
int i ;
for(i=s->top2;i<s->capasity;i++)
cout<<s->array[i]<<" " ;
}
int isfull_stack(stack *s)
{
if(s->top1+1>=s->top2)
return 1 ;
return 0 ;
}
int isempty_stack(stack *s)
{
if(s->top1==-1&&s->top2==s->capasity)
return 1 ;
return 0 ;
}
---------------------------------------------------------------------------------
Your task is to implement 2 stacks in one array efficiently .
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 4 Types
(i) 1 1 x (a query of this type means pushing 'x' into the stack 1)
(ii) 1 2 (a query of this type means to pop element from stack1 and print the poped element)
(iii) 2 1 x (a query of this type means poushing 'x' into the stack 2)
(iv) 2 2 (a query of this type means to pop element from stack2 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 stack is empty else the element poped out from the stack.
You are required to complete the 4 methods push1, push2 which takes one argument an integer 'x' to be pushed into the stack one and two and pop1, pop2 which returns a integer poped out from stack one and two .
Constraints:
1<=T<=100
1<=Q<=100
1<=x<=100
Example:
Input
1
6
1 1 2 1 1 3 2 1 4 1 2 2 2 2 2
Output
3 4 -1
Explanation:
In the first test case for query
1 1 2 the stack1 will be {2}
1 1 3 the stack1 will be {2,3}
2 1 4 the stack2 will be {4}
1 2 the poped element will be 3 from stack1 and stack1 will be {2}
2 2 the poped element will be 4 from stack2 and now stack2 is empty
2 2 the stack2 is now empty hence -1 .
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*The structure of the class is
class twoStacks
{
int *arr;
int size;
int top1, top2;
public:
twoStacks(int n=100){size = n; arr = new int[n]; top1 = -1; top2 = size;}
void push1(int x);
void push2(int x);
int pop1();
int pop2();
};
*/
/* The method push to push element into the stack 2 */
void twoStacks :: push1(int x)
{
if(top1>=top2)
return ;
arr[++top1]=x ;
}
/* The method push to push element into the stack 2*/
void twoStacks ::push2(int x)
{
if(top1>=top2)
return ;
arr[--top2]=x ;
}
/* The method pop to pop element from the stack 1 */
int twoStacks ::pop1()
{
if(top1==-1)
return -1 ;
else
return(arr[top1--]) ;
}
/* The method pop to pop element from the stack 2 */
int twoStacks :: pop2()
{
if(top2==size)
return -1 ;
else
return(arr[top2++]) ;
}
--------------------------------------------------------------------------------
COMPLETE STACK IMPLEMENTATION IN ARRAY :
--------------------------------------------------------------------------------
// implementing two stack in a single array
#include<iostream>
using namespace std ;
#include<malloc.h>
typedef struct STACK
{
int top1,top2 ;
int capasity ;
int *array ;
}stack ;
void create_stack(stack **,int );
void push_stack1(stack **,int );
void push_stack2(stack **,int );
int pop_stack1(stack **);
int pop_stack2(stack **);
int isfull_stack(stack *);
int isempty_stack(stack *);
void display_stack1(stack *) ;
void display_stack2(stack *) ;
int main()
{
stack *s ;
int no,choice,i,ele ;
cout<<"\n enter the capacity of the stack " ;
cin>>no ;
create_stack(&s,no) ;
do
{
cout<<"\n 1. push elemet in the first stack " ;
cout<<"\n 2. push elemet in the second stack " ;
cout<<"\n 3. pop element from the first stack " ;
cout<<"\n 4. pop element from the second stack " ;
cout<<"\n 5. display first stack " ;
cout<<"\n 6. display second stack " ;
cout<<"\n enter choice" ;
cin>>choice ;
switch(choice)
{
case 1 :cout<<"\n enter no of elemets to push " ;
cin>>no ;
cout<<"\n enter th elemets " ;
for(i=0;i<no;i++)
{
cin>>ele ;
push_stack1(&s,ele) ;
}
break ;
case 2 :cout<<"\n enter no of elemets to push " ;
cin>>no ;
cout<<"\n enter th elemets " ;
for(i=0;i<no;i++)
{
cin>>ele ;
push_stack2(&s,ele) ;
}
break ;
case 3 :ele=pop_stack1(&s) ;
if(ele!=-1)
cout<<"\n elemet poped is = "<<ele ;
break ;
case 4 :ele=pop_stack1(&s) ;
if(ele!=-1)
cout<<"\n elemet poped is = "<<ele ;
break ;
case 5 :display_stack1(s) ;
break ;
case 6 :display_stack2(s) ;
break ;
default : cout<<"\n TRY AGAIN !!!!" ;
}
cout<<"\n enter 1 to continue " ;
cin>>no ;
}while(no==1) ;
return 0 ;
}
void create_stack(stack **s,int no)
{
(*s)=(stack*)malloc(sizeof(stack)) ;
(*s)->top1=-1;
(*s)->top2=no ;
(*s)->capasity=no ;
(*s)->array=(int*)malloc((*s)->capasity*(sizeof(int))) ;
}
void push_stack1(stack **s,int data)
{
if(isfull_stack(*s))
{
cout<<"\n SORRY !!! can't take more input !!! STACK FULLL" ;
return ;
}
(*s)->array[++((*s)->top1)]=data ;
}
void push_stack2(stack **s,int data)
{
if(isfull_stack(*s))
{
cout<<"\n SORRY !!! can't take more input !!! STACK FULLL" ;
return ;
}
(*s)->array[--((*s)->top2)]=data ;
}
int pop_stack1(stack **s)
{
if(isempty_stack(*s))
{
cout<<"\n SORRY !!! stack is empty !! no elemet to POP" ;
return -1 ;
}
return((*s)->array[((*s)->top1)--]) ;
}
int pop_stack2(stack **s)
{
if(isempty_stack(*s))
{
cout<<"\n SORRY !!! stack is empty !! no elemet to POP" ;
return -1 ;
}
return((*s)->array[((*s)->top2)--]) ;
}
void display_stack1(stack *s)
{
if(isempty_stack(s))
{
cout<<"\n SORRY !!! stack is empty !! no elemet to display" ;
return ;
}
int i ;
for(i=0;i<=s->top1;i++)
cout<<s->array[i]<<" " ;
}
void display_stack2(stack *s)
{
if(isempty_stack(s))
{
cout<<"\n SORRY !!! stack is empty !! no elemet to display" ;
return ;
}
int i ;
for(i=s->top2;i<s->capasity;i++)
cout<<s->array[i]<<" " ;
}
int isfull_stack(stack *s)
{
if(s->top1+1>=s->top2)
return 1 ;
return 0 ;
}
int isempty_stack(stack *s)
{
if(s->top1==-1&&s->top2==s->capasity)
return 1 ;
return 0 ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment