In this program we will discuss about working of stack and we will see how to insert and delete data from stack.
//Program starts
#include<iostream>
#include<conio.h>
using
namespace std;
class
stack{
private:
int d,n,a[5],top;
public:
stack();
void create();
void push();
void pop();
void show();
};
stack::stack()
{
n=5;
top=-1;
}
void
stack::create()
{
for(int i=0;i<n;i++)
{
cout<<"Enter data
at ["<<i<<"] : ";
cin>>a[i];
}
top=4;
}
void
stack::push()
{
if(top==4)
{
cout<<"\n Stack
is full \n";
}
else
{
cout<<"Enter data
: ";
cin>>d;
top++;
a[top]=d;
}
}
void
stack::pop()
{
if(top==-1)
{
cout<<"\n Stack
underflow \n";
}
else
{
top--;
}
}
void
stack::show()
{
if(top==-1)
{
cout<<"\nStack is
empty\n ";
}
else
{
for(int i=0;i<=top;i++)
{
cout<<"Data at
["<<i<<"] = "<<a[i]<<endl;
}
}
}
int
main()
{
int c;
stack s1;
s1.create();
do{
cout<<"\nPress 1
to insert data\nPress 2 to delete data\n Press 3 to show data\nPress 4 to
exit\nValue = ";
cin>>c;
switch(c)
{
case 1:
s1.push();
break;
case 2:
s1.pop();
break;
case 3:
s1.show();
break;
case 4:
return
0;
break;
}
}
while(1);
}
OUTPUT:
0 comments:
Post a Comment