HomeData Structures

What is Push function in Simple Stack ?

What is Push function in Simple Stack ?
Like Tweet Pin it Share Share Email

Simple Stack :Stack is an ordered list of similar data type. Stack is a LIFO structure. (Last in First out). push() function is used to insert new elements into the Stack and pop() is used to delete an element from the stack. Both insertion and deletion are allowed at only one end of Stack called Top.

Simple Stack

Push Refers as “Adding Elements onto Stack“.
Push Operation carried out in following

2 steps –

First Increment Variable “top“ so that it now refers to next memory location.
Secondly Add Element Onto Stack by accessing array.

void Push()
{ if(i>=5)
cout<<"Array is Full";
else{
for(int j=i;j>=0;j--)
{ num[j+1]=num[j]; }
cout<<"Enter Value: ";
cin>>num[0]; i++;
}}}<p/re>

Pop function in simple Stack

void Pop(){
if(i==0)
cout<<"Error!There is no Value"<<endl;
else
{for(int j=0;j<i;j++)
{num[j]=num[j+1];}
i--;}}

Display function in simple Stack

void show(){
if(i==0)
cout<<"Array is Empty"<<endl;
else
{
for(int j=0;j<i;j++)
{ cout<<num[j]<<endl;
}}}

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *