Stack Program.
#include<iostream>
using namespace std;
void push(int ele);
int pop();
int peep(int pos);
void change(int pos,int ele);
void display();
#define max 5
int s[5];
int top=-1;
int main()
{
int ch,ele;
int ans,pos;
while(1)
{
cout<<"\n press 1 for push: ";
cout<<"\n press 2 for pop: ";
cout<<"\n press 3 for peep: ";
cout<<"\n press 4 for change: ";
cout<<"\n press 5 for display: ";
cout<<"\n press 6 for exit: ";
cout<<"\n enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n enter the element: ";
cin>>ele;
push(ele);
break;
case 2:
ans=pop();
if(ans!=-1)
cout<<"\n"<<ans<<"is deleted";
break;
case 3:
cout<<"\n enter the position: ";
cin>>pos;
ans=peep(pos);
if(ans!=-1)
cout<<"\n element is: "<<ans;
break;
case 4:
cout<<"\n enter the poition: ";
cin>>pos;
cout<<"\n enter the element: ";
cin>>ele;
change(pos,ele);
break;
case 5:
display();
break;
case 6:
exit(0);
break;
default:
cout<<"\n invalid input";
}
}
return 0;
}
void push(int ele)
{
if(top==max-1)
{
cout<<"\n stsck is overflow";
}
else
{
top=top+1;
s[top]=ele;
}
}
int pop()
{
int ans;
if(top==-1)
{
cout<<"\nstack is underflow";
return -1;
}
else
{
ans=s[top];
top=top-1;
return ans;
}
}
int peep(int pos)
{
int ans;
if(top-pos+1<0)
{
cout<<"\n invalid position";
return -1;
}
else
{
ans=s[top-pos+1];
return ans;
}
}
void change(int pos,int ele)
{
if(top-pos+1<0)
{
cout<<"\n vaild position";
}
else
{
s[top-pos+1]=ele;
}
}
void display()
{
int i;
if(top==-1)
{
cout<<"\n there are no elements";
}
else
{
for(i=0;i<=top;i++)
{
cout<<" "<<s[i];
}
}
}
0 Comments