0% found this document useful (0 votes)
4 views

Array Implementation of Stacks

hi

Uploaded by

gnprints
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Array Implementation of Stacks

hi

Uploaded by

gnprints
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

/*************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;
}
}
}

You might also like