C++ program for implementation of Stack (push,pop & display) operation.Stack based on the LIFO (Last In First Out) behavior means, the last
element is pushed(insert) inside the stack , is the first element to get
pop(deleted).Stack is a data structure in which the objects are arranged in a non linear order.
In stack, elements are added or deleted from only one end, i.e. top of the stack.
Here we implement the PUSH, POP, DISPLAY stack operations using the array.
Source Code:-
#include<iostream.h>
#include<conio.h>
int stack[20],top=-1; //global declaration
void push();
void pop();
void display();
void main()
{ clrscr();
int ch;
cout<<"Program for Stack Operations by-Tarun Rawat\n\n";
do
{ cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter your choice : ";
cin>>ch;
if(ch==1)
push();
else if(ch==2)
pop();
else if(ch==3)
display();
else if(ch==4)
cout<<"\nEnd Of program";
}while(ch!=4);
}
//to push elements in a stack
void push()
{
if(top>9)
cout<<"\nStack Overflow";
else
{ top=top+1;
cout<<"\nEnter a value : ";
cin>>stack[top];
}
}
//to pop/delete stack elements
void pop()
{
if(top==-1)
cout<<"\nStack Overflow. No Elements to pop";
else
{
cout<<stack[top]<<" is deleted..";
top--;
}
}
//displaying stack elements
void display()
{
int i;
if(top==-1)
cout<<"\nStack underflow. No elements to display..";
else
{
cout<<"\nThe stack elements are : ";
for(i=0;i<=top;i++)
{
cout<<"\t"<<stack[i];
}
cout<<"\n";
}
}
Welcome to " way2cplusplus.blogspot.in " Objective of this blog is to implement various Computer Science Engineering Lab problems into C++ programming language. These are basically most common Lab Exercise problems based on the curriculum of engineering colleges throughout the Nation. These lab exercises are also relevant to Data structure. Simply C++ programming zone...
Pop operation in Stack
ReplyDeleteThis operation removes the data value on top of the stack. Your artile about Stack POP Operation in C++ is helpful for me and all college strudents.