Advertisement

STACK


#include<iostream>
using namespace std;
#define MAX 10 // size of the array
int stack[MAX];
int top=0;
void push();
void pop();
void display();
int main()
{
int option=1;
int i;
char choice;
do
{
cout<<"\nSelect stack Operation :\n\n1. PUSH\n2. POP\n3. EXIT\n\n";
cout<<"Enter your choice: ";
cin>>option;
switch(option)
{
case 1:
push();
cout<<"\nElements in stack after PUSH operation: \n";
display();
break;
case 2:
pop();
cout<<"\nElements in stack after POP operation: \n";
display();
break;
case 3:
exit(0);
break;
default:
printf("Wrong Option . ");
}
cout<<"\nDo you Repeat? Enter y/n or Y/N: ";
cin>>choice;
}while(choice == 'y' || choice == 'Y');
cout<<"\n Program END "<<endl;
system("PAUSE");
return 0;
}
void push()
{
int no;
cout<<"\n\nHow many numbers u want to insert: >> ";
cin>>no;
for( int j = 1; j<=no; j++)
{
int n;
if(top>=MAX)
cout<<"\nSTACK FULL";
else
{
if(top<0)
top=0;

cout<<j<<": Element in stack : ";
cin>>n;
stack[top++]=n;
}
}
}

void pop()
{

int e;
cout<<"\nHow many elements you want to POP: ";
cin>>e;
for(int k = 1; k<=e; k++)
{
if(top>=0)
stack[top--];
}
}

void display()
{

if(top<=0)
cout<<"\n\nSTACK is EMPTY";
else

for(int i=top-1;i>=0;i--)
{
cout<<stack[i]<<endl;
}

}

OUTPUT