16. WAP to reverse the order of the elements in the stack using additional stack.
PROGRAME CODE
#include<stdio.h>
#include<iostream>
using namespace std;
class stack_rev{
int
size,*arr,*arr1,top,top1;
public:
stack_rev(){
size=0;
arr=NULL;
arr1=NULL;
top=-1;
top1=-1;
}
void
getdata(){
cout<<"ENTER
THE SIZE OF THE STACK-> "<<endl;
cin>>this->size;
arr=new
int[this->size];
arr1=new
int[this->size];
}
void
push(){
if(top==(size-1))
cout<<"THE
STACK IS OVER-FLOW...."<<endl;
else{
int
x=0;
cout<<"ENTER
THE ELEMENT ->"<<endl;
cin>>x;
top++;
arr[top]=x;
}
}
void
revstack(){
if(top1==(size-1))
cout<<"THE
STACK IS OVER-FLOW...."<<endl;
else{
while(top>=0){
top1++;
arr1[top1]=arr[top];
top--;
}
}
}
void
display1(){
cout<<"THE
ORIGINAL STACK IS ->"<<endl;
int
x=top+1;
while(--x>=0)
cout<<arr[x]<<endl;
}
void
display2(){
cout<<"THE
REVERSE STACK IS ->"<<endl;
int
x=top1+1;
while(--x>=0)
cout<<arr1[x]<<endl;
}
};
int main(){
stack_rev
ob;
int
opt=0;
do{
cout<<"\nPRESS
0 TO EXIT ...."<<endl;
cout<<"PRESS
1 TO SET STACK SIZE...."<<endl;
cout<<"PRESS
2 TO PUSH DATA...."<<endl;
cout<<"PRESS
3 TO DISPLAY THE STACK...."<<endl;
cout<<"PRESS
4 TO REV THE STACK...."<<endl;
cout<<"PRESS
5 TO DISPLAY THE REV STACK....."<<endl;
cin>>opt;
switch(opt){
case
0:
break;
case
1:
ob.getdata();
break;
case
2:
ob.push();
break;
case
3:
ob.display1();
break;
case
4:
ob.revstack();
cout<<"THE
REVERSE SUCCESSFULL.........."<<endl;
break;
case
5:
ob.display2();
break;
default:
cout<<"ERROR
OPTION CHOICE ........."<<endl;
break;
}
}while(opt!=0);
return
0;
}
No comments:
Post a Comment