skip to main |
skip to sidebar
#include<iostream.h>
#include<conio.h>
class stack
{
int top,n,a[50],i;
public:
void getsize();
void push();
void pop();
void display();
};
void stack::getsize()
{
top=-1;
cout<<"enter the size\n";
cin>>n;
}
void stack::push()
{
if(top==n-1)
{
cout<<"over flow\n";
}
else
{
cout<<"enter element\n";
top=top+1;
cin>>a[top];
}}
void stack::pop()
{
if(top==-1)
{
cout<<"under flow\n";
}
else
top--;
}
void stack::display()
{
cout<<"elements in the stack are\n";
for(i=top;i>=0;i--)
{
cout<<"\n"<<a[i];
}}
void main()
{
clrscr();
int c;
stack ob;
ob.getsize();
do
{
cout<<"menu\n1-push\n2-pop\n3-display\n4-exit\n";
cin>>c;
switch(c)
{
case 1:ob.push();
break;
case 2:ob.pop();
break;
case 3:ob.display();
break;
}}
while(c<=3);
getch();
}
OUTPUT:
enter the size
3
menu
1-push
2-pop
3-display
4-exit
1e
Enter the element
48
7
menu
1-push
2-pop
3-display
4-exit
1
overflow
menu
1-push
2-pop
3-display
4-exit
3e
Elements in the stack are
784
menu
1-push
2-pop
3-display
4-exit
2
menu
1-push
2-pop
3-display
4-exit
3e
Elements in the stack are
84
0 comments:
Post a Comment