#include<iostream.h>
#include<conio.h>
#include<process.h>
#define max 20
int top;
class stack
{
int
a[max],n,item;
public:
stack()
{
top=0;
}
void
initial_stack();
void
push(int);
int
pop();
void
display();
};
void
stack::initial_stack()
{
char c;
cout<<"\nDo
you want initialy elements inserted in the stack(y/n):-";
cin>>c;
if(c=='y')
{
cout<<"How
many elements you want initialy in the stack"<<endl;
cin>>n;
cout<<"enter
elements\n";
for(int
i=1;i<=n;i++)
{
top=top+1;
cin>>item;
a[top]=item;
}
}
}
void
stack::push(int m)
{
if(top==max)
{
cout<<"\nCONDITION
OF OVERFLOW";
getch();
exit(1);
}
else
{
top=top+1;
a[top]=m;
}
}
int
stack::pop()
{
if(top==0)
{
cout<<"\nCONDITION OF
UNDERFLOW";
getch();
exit(1);
}
else
{
item=a[top];
top=top-1;
}
return
item;
}
void
stack::display()
{
cout<<"\nTop
="<<top;
cout<<"\nstack["<<top<<"]="<<a[top];
}
void main()
{
clrscr();
stack
obj;
char
ch='y';
int
choice,element;
obj.initial_stack();
while(ch=='y')
{
cout<<"What
you want to do :-Enter ur choice"<<endl;
cout<<"1.Push
in stack\n2.Pop from stack\n";
cout<<"3.Display(peek)
the TOP of stack\n4.exit";
cin>>choice;
switch(choice)
{
case 1:
cout<<"enter
the element to push"<<endl;
cin>>element;
obj.push(element);
cout<<"After
push:-"<<endl;
obj.display();
break;
case 2:
element=obj.pop();
cout<<"After
pop
of"<<element<<"from"<<top+1<<"location:-"<<endl;
obj.display();
break;
case 3:
obj.display();
break;
default:
exit(1);
}
cout<<endl<<"Do you
want to continue(y/n)";
cin>>ch;
}
getch();
}
No comments:
Post a Comment