Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 3

/*MOVING ONE STACK TO ANOTHER*/

/*...........................*/

#include<iostream.h>
#include<conio.h>
class stacks
{
int n;
public:
int stk1[50],top,stk2[50],t[50];
stacks()
{
top=0;
}
void create();
void push();
void move(int top,int stk1[],int t[],int stk2[]);
void display();
};
void stacks::create()
{
cout<<"\n Enter the size of stack1:";
cin>>n;
cout<<"\n The size of stack2 is also:"<<n<<endl;
}
void stacks::push()
{
cout<<"\n Enter the stack1 elements:"<<endl;
for(int i=1;i<=n;i++)
{
cin>>stk1[++top];
}
}
void stacks::move(int top,int stk1[],int t[],int stk2[])
{
if(top==1)
{
stk2[top]=stk1[top];
return;
}
move(top-1,stk1,stk2,t);
stk2[top]=stk1[top];
move(top-1,t,stk1,stk2);
return;
}

/conversion/tmp/scratch/441238506.rtf 13CS130
void stacks::display()
{
int i;
if(top>0)
{
cout<<"\n First stack elements are:";
for(i=top;i>0;i--)
{
cout<<"\n\t"<<stk1[i]<<endl;
}
cout<<"\n Second stack elements are:";
for(i=top;i>0;i--)
{
cout<<"\n \t"<<stk2[i]<<endl;
}
}
else
{
cout<<"\n No Elements";
}
}
void main()
{
int x;
clrscr();
stacks s;
cout<<"\n\t\t\tMOVING STACKS";
do
{
cout<<"\nDo the following"<<endl;
cout<<"1.Create"<<endl;
cout<<"2.Push"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter the option:";
cin>>x;
switch(x)
{
case 1:s.create();break;
case 2:s.push();
s.move(s.top,s.stk1,s.t,s.stk2);break;
case 3:s.display();break;
default:cout<<"\nBYE....";
}
}
while(x<4);

/conversion/tmp/scratch/441238506.rtf 13CS130
getch();
}

MOVING STACKS
Do the following
1.Create
2.Push
3.Display
4.Exit
Enter the option:1
Enter the size of stack1:3
The size of stack2 is also:3

Do the following
1.Create
2.Push
3.Display
4.Exit
Enter the option:2
Enter the stack1 elements:
11
22
33

Do the following
1.Create
2.Push
3.Display
4.Exit
Enter the option:3
First stack elements are:
33
22
11
Second stack elements are:
33
22
11

Do the following
1.Create
2.Push
3.Display
4.Exit
Enter the option:4
BYE....

/conversion/tmp/scratch/441238506.rtf 13CS130

You might also like