This C++ program implements the following stack operations.
Push
Pop
Top
Empty
STEPS:
Ø Create an array to store the stack elements.
Ø Get the size of the stack.
Ø To push an element into the stack check if the top element is less than the size and increment
the top.
Ø Else print overflow .
Ø To pop an element, check if the stack is empty and decrement the stack.
Ø If all the elements are popped, print underflow.
Ø Find the topmost element in the stack by checking if the size is equal to top.
Ø If the stack is empty print empty, else print not empty.
CODING:
#include<iostream.h>
#include<conio.h>
int max=7;
int t=0;
class stack
{
int s[7];
public:
void push(int);
void pop();
void top();
void empty();
void show();
};
void stack::push(int y) //Push Operation
{
if(t<max)
{
t=t+1;
s[t]=y;
}
else
cout<<endl<<"stack overflows..."<<endl;
}
void stack::pop() //Pop Operation
{
int item;
if(t>=0)
{
t=t-1;
item=s[t+1];
cout<<endl<<"popped item >>"<<item<<endl;
}
else
cout<<endl<<"stack underflows"<<endl;
}
void stack::top() //To find the top of the stack
{
if(t>=0)
cout<<endl<<"topmost element >> "<<s[t]<<endl;
else
cout<<endl<<"stack underflows..."<<endl;
}
void stack::empty() //To check if the stack is empty
{
if(t<0)
cout<<endl<<"stack is empty..."<<endl;
else
cout<<endl<<"stack is not empty..."<<endl;
}
void main()
{
int a,x;
stack s1;
clrscr();
do
{
cout<<"enter an option..."<<endl<<"1-push"<<endl<<"2-pop"<<endl<<"3-top"<<endl<<"4-empty"<<endl;
cout<<"5-end"<<endl;
cin>>a;
cout<<endl;
switch(a)
{
case 1:
{
cout<<endl<<"enter a value >> "<<endl;
cin>>x;
s1.push(x);
}
break;
case 2:
s1.pop();
break;
case 3:
s1.top();
break;
case 4:
s1.empty();
break;
}
}
while(a!=5);
getch();
}
Tuesday, 7 July 2015
C++ Program - Implementation of Stack
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment