Lab#6 - Stack ADT
Lab#6 - Stack ADT
STACK: A stack is an ordered collection of data items into which new items may be inserted
and from which data items may be deleted at one end. Stacks are also called Last- In-First-out
(LIFO) lists.
Representation of a Stack
#include<iostream>
using namespace std;
#define MAX 10
int top=-1,ch,i;
int stk[MAX], ele;
void Push()
{
if(top==(MAX-1))
cout<<"\n\nThe stack is full";
else
{
cout<<"\n\nEnter an element:";
cin>>ele;
top++;
stk[top]=ele;
cout<<"\n\nElement pushed successfully\n";
}
}
void Pop()
{
if(top==-1)
cout<<"\n\nThe stack is empty";
else
{
ele=stk[top];
top--;
cout<<"\n\nThe deleted element is:"<<ele;
}
}
void Top()
{
if(top==-1)
cout<<"\n\nThe stack is empty";
else
cout<<"The top element of the stack is:"<<stk[top];
}
void Display()
{
if(top==-1)
cout<<"\n\nThe stack is empty";
else
{
cout<<"\n\nThe elements in the stack are:";
for(i=top;i>=0;i--)
cout<<"\n"<<stk[i];
}
}
int main()
{
bool flag=true;
do
{
cout<<"\n****MENU****";
cout<<"\n1. Push\n2. Pop\n3. Top\n4. Display\n5. Exit";
cout<<"\nEnterur Choice:";
cin>>ch;
switch(ch)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: Top();
break;
case 4: Display();
break;
case 5: flag=false;
default: cout<<"Enter correct Choice";
}
}while(flag);
}