Sunday, December 12, 2010

Stack Using Array

//Program to implement stack using array

#include
#include
#include

#define SIZE 20

class stack
{
int arr[SIZE],top,ele;
public:
stack();
void push();
void pop();
void display();
};

stack::stack()
{
top=-1;
}

void stack::push()
{
if(top>SIZE-1)
{
cout<<"\nCannot perform push operation,Stack overflow!!!";
}
else
{
cout<<"\nEnter the element to be inserted";
cin>>ele;
arr[++top]=ele;
}
}

void stack::pop()
{
if(top==-1)
{
cout<<"\nCannot perform pop operation,Satck underflow!!!";
}
else
{
cout<<"\nThe element popped is "< }
}

void stack::display()
{
int i;
if(top==-1)
{
cout<<"\nStack is empty!!!!!";
}
else
{
cout<<"\nThe Stack is now\n";
for(i=top;i>=0;i--)
cout<";
}
}

void main()
{
stack s1;
int ch;
clrscr();
do
{
cout<<"\n------------Stack Operations-------------";
cout<<"\n1_PUSH";
cout<<"\n2_POP";
cout<<"\n3_DISPLAY";
cout<<"\n4_EXIT";
cout<<"\nEnter choice<1-4>:";
cin>>ch;

switch(ch)
{
case 1:
s1.push();
break;

case 2:
s1.pop();
break;

case 3:
s1.display();
break;

case 4:
exit(0);
break;

default:
cout<<"\nInvalid choice!!!!!";
break;
}
}while(ch!=4);
getch();
}

No comments:

Post a Comment