#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
class stack
{
private:
int
a[100],top,value,size,i,t,temp,ele;
public:
void push(int);
void pop(int);
void
display(int);
};
void
stack::push(int size)
{
i=1;
top=-1;
for(i=0;i<size;i++)
{
cout<<"\nEnter element ";
cin>>value;
if(value>=size)
{
cout<<"\nSTACK OVERFLOW:-
"<<"("<<value<<" >=
"<<size<<")";
break;
}
else
{
top++;
a[top]=value;
cout<<"\nYou
Push:->"<<a[top];
}
}
}
void
stack::pop(int size)
{
if(top<1)
{
cout<<"\nSTACK UNDERFLOW";
}
else
{
cout<<"\nHow many Elements do
want to POP..? ";
cin>>ele;
while(ele>0)
{
temp=a[top];
cout<<"\nYou
POP:->"<<a[top];
top--;
ele--;
}
}
}
void
stack::display(int size)
{
cout<<"\nSTACK
ELEMENTS ARE:-\n";
while(top>=0)
{
cout<<a[top]<<endl;
top--;
}
}
void main()
{
stack ob;
int
value,size,option,i;
char ch='y';
clrscr();
while(ch=='y')
{
cout<<"\n*****************";
cout<<"\n1.PUSH
AN ELEMENT";
cout<<"\n2.POP AN ELEMENT";
cout<<"\n3.DISPLAY
STACK";
cout<<"\n4.Exit";
cout<<"\n*****************";
cout<<"\nChoose
any option ";
cin>>option;
switch(option)
{
case 1:
cout<<"\nEnter size of Stack
";
cin>>size;
ob.push(size);
break;
case 2:
ob.pop(size);
break;
case 3:
ob.display(size);
break;
case 4:
exit(0);
}
cout<<"\n\nContinue..?(y/n)\n";
cin>>ch;
}
getch();
}
Comments
Post a Comment