Implementation of Stack Using Array.

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

STACKWIT.

CPP

September 9, 2013

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class stackop
{
private :
int tos,st[10];
public :
stackop();
int push(int x);
int pop();
int is_full();
int is_empty();
void display();
};
stackop::stackop()
{
tos=-1;
}
int stackop::push(int x)
{
if(is_full())
{
tos++;
st[tos]=x;
return x;
}
else
{
cout<<"Stack is full, cannot push "<<x;
return -1;
}
}
int stackop::pop()
{
if(is_empty())
{
int y;
y=st[tos];
tos--;
return y;
}
else
{
cout<<"\t\tStack is empty,element can't be poped out\n\n";
return -1;
}
}
int stackop::is_full()
{
if(tos==9)
return 0;
else
return 1;
}
int stackop::is_empty()
{
if(tos==-1)
return 0;
else
return 1;
}
void stackop::display()
{
if(tos==-1)
cout<<"\t\tstack is empty, nothing to display!!\n\n";
else

Page 1

STACKWIT.CPP

September 9, 2013

{
cout<<"\t\tElements in stack are:\n\n";
for (int i=tos;i>=0;i--)
{
cout<<"\t\t "<<st[i]<<" ";
}
}
}
int main()
{
int choice,item;
stackop obj;
while (1)
{

cout<<"\n\t\t---------------------------------\n";
cout<<"\t\t| STACK OPERATIONS USING ARRAY
|\n";
cout<<"\t\t---------------------------------\n";
cout<<"\t\t| 1) PUSH
|\n";
cout<<"\t\t| 2) POP
|\n";
cout<<"\t\t| 3) DISPLAY
|\n";
cout<<"\t\t| 4) EXIT
|\n";
cout<<"\t\t---------------------------------\n";
cout<<"\t\tEnter your Choice : ";
cin>>choice;
switch (choice)
{
case 1 : cout<<"\n\n\t\t*****PUSH*****\n";
cout<<"\t\tEnter item to be pushed : ";
cin>>item;
item=obj.push(item);
if(item!=-1)
{
cout<<"\n\t\tItem being pushed is : "<<item;
getch();
}
break ;
case 2 : cout<<"\n\n\t\t*****POP*****\n";
item=obj.pop();
if(item!=-1)
{
cout<<"\n\t\tItem being poped out is : "<<item;
getch();
}
break ;
case 3 : cout<<"\n\n\t\t*****DISPLAY*****\n";
obj.display();
break ;
case 4 : exit(0);
break ;
default : cout<<"\n\n\t\tInvalid Choice\n\n";
getch();
break ;
}
}

Page 2

You might also like