Push Operation
The process of putting a new data element onto stack is known as a Push Operation. Push operation involves a series of steps −
- Step 1 − Checks if the stack is full.
- Step 2 − If the stack is full, produces an error and exit.
- Step 3 − If the stack is not full, increments top to point next empty space.
- Step 4 − Adds data element to the stack location, where top is pointing.
- Step 5 − Returns success.
-
If the linked list is used to implement the stack, then in step 3, we need to allocate space dynamically.
Algorithm for push:
PUSH(Stack, Top, MAX Stack, Item)1.Stack is already filled?
If top=MAX STK Print :
Over flow and Return
2.Set top=top+1
3.Set stack[top]=item
4.return
Pop Operation
Accessing the content while removing it from the stack, is known as a Pop Operation. In an array implementation of pop() operation, the data element is not actually removed, instead top is decremented to a lower position in the stack to point to the next value. But in linked-list implementation, pop() actually removes data element and deallocates memory space.
A Pop operation may involve the following steps −
-
Step 1 − Checks if the stack is empty.
-
Step 2 − If the stack is empty, produces an error and exit.
-
Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
-
Step 4 − Decreases the value of top by 1.
-
Step 5 − Returns success.
-
Algorithm for pop:
- POP(Stack,top,Item)
- 1.Stack has an item to be removed.
- If top=0
- Print :underflow and return
- 2.Set item =stack[top]
- 3.Set top=top-1Return
Source code:
#include<iostream>
#include<conio.h>
#define size 5
using namespace std;
int stack[size],i,push_element,pop_element,top=0,re_push;
void push(int m)
{
if(top==(size-1))
{
cout<<"stack overflow:";
}
else
for(i=0;i<m;i++)
{
cout<<"Enter "<<i<<" th element :";
cin>>stack[i];
top=top+1;
}
cout<<"pushing "<<m<<"element on array :"<<endl;
cout<<"pushed element on array are :"<<endl;
for(i=0;i<m;i++)
{
cout<<i<<" -position:"<<stack[i]<<endl;
}
}
void pop(int n)
{
cout<<"poping element from array"<<endl;
for(i=0;i<(push_element-n);i++)
{
cout<<i<<"-position"<<stack[i]<<endl;
}
}
int main()
{ cout<<"size of your stack is :"<<size<<endl<<"follow following instruction:"<<endl;
a:
cout<<"Enter how many element you want to push on stack:";
cin>>push_element;
if(push_element>size)
{cout<<"sorry size of stack is only 5,re-enter no of element u want to pushed:"<<endl;
goto a;
}
push(push_element);
cout<<"Enter how many element you want to pop from stack:";
cin>>pop_element;
pop(pop_element);
cout<<"If u want to re-push data on stack press 1:";
cin>>re_push;
if(re_push==1)
{
goto a;
}
getch();
return 0;
}
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
Algorithm for pop:
#include<iostream>
#include<conio.h>
#define size 5
using namespace std;
int stack[size],i,push_element,pop_element,top=0,re_push;
void push(int m)
{
if(top==(size-1))
{
cout<<"stack overflow:";
}
else
for(i=0;i<m;i++)
{
cout<<"Enter "<<i<<" th element :";
cin>>stack[i];
top=top+1;
}
cout<<"pushing "<<m<<"element on array :"<<endl;
cout<<"pushed element on array are :"<<endl;
for(i=0;i<m;i++)
{
cout<<i<<" -position:"<<stack[i]<<endl;
}
}
void pop(int n)
{
cout<<"poping element from array"<<endl;
for(i=0;i<(push_element-n);i++)
{
cout<<i<<"-position"<<stack[i]<<endl;
}
}
int main()
{ cout<<"size of your stack is :"<<size<<endl<<"follow following instruction:"<<endl;
a:
cout<<"Enter how many element you want to push on stack:";
cin>>push_element;
if(push_element>size)
{cout<<"sorry size of stack is only 5,re-enter no of element u want to pushed:"<<endl;
goto a;
}
push(push_element);
cout<<"Enter how many element you want to pop from stack:";
cin>>pop_element;
pop(pop_element);
cout<<"If u want to re-push data on stack press 1:";
cin>>re_push;
if(re_push==1)
{
goto a;
}
getch();
return 0;
}