Write a program to implement push and pop operations on a stack using linear array.

#include<iostream>
using namespace std;
int stack[10]={27,56,43,87,64};
int top=4;
void push();
int pop();
void display();
int main(){
int a,o;
cout<<"Stack is:";
display();
cout<<"\n\n1. for PUSH\n2. for POP\n3. for DISPLAY\nEnter your choice:";
cin>>a;
switch(a){
case 1:push();
  cout<<"\nStack after operation is:";
  display();
  break;
case 2:cout<<"\nElement POPED is:"<<pop();
  cout<<"\nStack after operation is:";
  display();
  break;
case 3:display();
  break;
}
cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";
return 0;
}
void push(){
if(top==9){
cout<<"\nStack is full";
exit(1);
}
else{
top=top+1;
cout<<"\nEnter the Element:";
cin>>stack[top];
}
}
int pop(){
int item;
if(top==-1){
cout<<"\nStack is empty";
exit(1);
}
else{
item=stack[top];
top=top-1;
return item;
}
}
void display(){
if(top==-1){
cout<<"\nStack is empty";
exit(1);
}
else{
for(int i=top;i>=0;i--)
cout<<endl<<stack[i];
}
}

Output:

  1. PUSH



  2. POP


  3. DISPLAY





























Comments

Popular posts from this blog

To create an html page with frames and frameset

1. Write a program to insert a new element at end as well as at a given position in an array in Java

1. To familiarize with network devices like switch, hub, routers and bridges in LaTex