Monday 22 August 2016

Stack Using Array

//Girfa Student Help stack using array
#include<iostream.h>
#include<conio.h>
#define MAX 5
void main()
{
int ar[MAX],top=-1,i,opt;
do
{
clrscr();
cout<<"\n\t1. Push\n\t2. Pop\n\t0. Exit\n\tEnter your choce>> ";
cin>>opt;
switch(opt)
{
case 1:

cout<<"Enter number>> ";
cin>>i;
if(top==MAX-1)
{
cout<<"Stack is full";
getch();
}
else if(top==-1)
{
top=0;
ar[top]=i;
}
else
{
top++;
ar[top]=i;
}
break;
case 2:
if(top==-1)
{
cout<<"Stack is empty";
getch();
}
else
{
cout<<"\n\tPop value is  "<<ar[top];
top--;
getch();
}
case 0:
break;
default:
cout<<"\n\tInvalid choice";
getch();

}
}while(opt!=0);

}

Next Topic

No comments:

Post a Comment