Array Implementation of Stacks
Array Implementation of Stacks
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define max 5
class stack
{
int top;
int arr[max];
public:
stack()
{
top=-1;
}
void push();
void pop();
void display();
};
void stack::push()
{
int value;
if(top==max-1)
{
cout<<"STACK IS FULL CANNOT INSERT!!"<<endl;
}
else
{
cout<<"ENTER THE VALUE: "<<"\t";
cin>>value;
top++;
arr[top]=value;
cout<<"THE VALUE INSERTED!!"<<endl;
}
}
void stack::pop()
{
int ele;
if(top==-1)
{
cout<<"STACK IS EMPTY!!"<<endl;
}
else
{
ele=arr[top];
top--;
cout<<"THE VALUE "<<ele<<" IS DELETED"<<endl;
}
}
void stack::display()
{
int i;
if(top==-1)
{
cout<<"NO ELEMENTS TO DISPLAY!!"<<endl;
}
else
{
cout<<"ELEMENTS ARE: "<<"\t";
for(i=0;i<=top;i++)
{
cout<<arr[i]<<"\t";
}
}
}
void main()
{
int choice;
clrscr();
stack ob;
cout<<"ARRAY IMPLEMENTATION OF STACK"<<endl;
cout<<"1->PUSH 2->POP 3->DISPLAY 4->EXIT"<<endl;
while(1)
{
cout<<"\nENTER YOUR CHOICE: "<<"\t";
cin>>choice;
switch(choice)
{
case 1:ob.push();
break;
case 2:ob.pop();
break;
case 3:ob.display();
break;
case 4:exit(0);
default:
cout<<"ENTER THE VALUE BETWEEN 1 TO 4: "<<endl;
}
}
}