Program that uses push and pop method of stack class in c++.....
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<graphics.h>
#define MAX 5
class stack
{
int top,i;
int arr[MAX],x;
char ch;
public :
stack();//DEFUALT ZERO CONSTRUCTOR
void creat();
void push();
void pop();
void deewan();
void outof();
};
stack::stack()//DEFINE CONSTRUCTOR
{
top=-1; //INITILISE THE STACK POINTER
}
void stack::deewan()
{
clrscr();
while(1)
{
cleardevice();//CLEAR THE GRAPHICS SCREEN
gotoxy(4,3);
cout<<"::GIVEN FUNCTIONS::==> ";
gotoxy(27,6);
cout<<"C :: CREAT A NEW FRESH STACK::::::::";
gotoxy(27,8);
cout<<"P :: PUSH OPRATION PERFORM ON STACK::";
gotoxy(27,10);
cout<<"O :: POP OPRATION PERFORM ON STACK:::";
gotoxy(27,12);
cout<<"E : EXIT:::::::::::::::::::::::::::::";
gotoxy(3,25);
cout<<"ENTER YOUR RIGHT CHOICE :-> ";
cin>>ch;
switch(ch)
{
case 'c':case 'C':
clrscr();
creat();
break;
case 'e':case 'E':
outof();
break;
case 'p':case 'P':
push();
break;
case 'o':case 'O':
pop();
break;
default :
cleardevice();//CLEAR THE GRAPHICS SCREEN
gotoxy(10,5);
cout<<"\n::CHOOSE RIGHT THINGS::\n";
break;
}
gotoxy(3,25);
getche();
}
}
void stack :: creat()
{
if(top<MAX-1)
{
champak:
cleardevice();//clear the graphics screen
cout<<"::ENTER THE ELEMENT IN STACK:: ";
for(i=0;i<MAX;i++)
{
cin>>arr[i];
top++;
}
cleardevice();
gotoxy(2,2);
cout<<"::THE CURRENT STATUS OF STACK IS::" ;
setcolor(4);
setbkcolor(2);
int r=0,b=0,j=0;
for(i=0;i<MAX;i++,r=r+30,b=b+30)
{
rectangle(165,45,207,75+b);
gotoxy(22,4+j);
cout<<arr[i];
j=j+2;
}
}
else
{
top=-1;
goto champak;
}
}
void stack::push()
{
cleardevice();
if(top>=MAX-1)
{
setcolor(15);
setbkcolor(4);
gotoxy(5,5);
cout<<"::STACK IS FULL i.e.OVERFLOW !!!!!!.............." ;
}
else
{ setcolor(15);
setbkcolor(4);
gotoxy(5,2);
cout<<"::ENTER AN ELEMENT TO BE PUSH:: ";
cin>>x;
arr[++top]=x;
cleardevice();
gotoxy(2,1);
cout<<"::AFTER PUSH AN ELEMENT:: ";
gotoxy(2,2);
cout<<"::THE CURRENT STATUS OF STACK IS::";
int r=0,b=0,j=0;
for(i=0;i<=top;i++,r=r+30,b=b+30)
{
rectangle(165,45,207,75+b);
gotoxy(22,4+j);
cout<<arr[i];
j=j+2;
}
}
}
void stack::pop()
{
int i,b=0,j=0,r=0;
cleardevice();
gotoxy(5,1);
if(top<=-1)
cout<<"\n::UNDERFLOW CONDITION IS APPLY:: ";
else
{
cout<<"::AFTER THE POP OPRATION THE ELEMENT OF STACK ARE::";
for(i=0;i<top;i++,r=r+30,b=b+30)
{
rectangle(165,45,207,75+b);
gotoxy(22,4+j);
cout<<arr[i];
j=j+2;
}
cout<<"\n\n\n\t::AND THE DELETED ELEMENT IS:::::::";
rectangle(165,95+b,207,95+b+30);
gotoxy(22,4+j+3);
cout<<arr[top];
j=j+2;
}
--top;
}
void stack::outof()
{
exit(0);
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd , &gm, "c:\\tc\\bgi");
setbkcolor(9);
stack s;
s.deewan();
closegraph();//shut down the graphics system
}