Push and Pop operation in Stack in Data Structure

What are Stacks ?

In Computer Science, Stacks are Abstract Data type and Linear Data Structure with a bounded capacity. Being a popular concept of Data Structures, there are several applications of stacks and operations that can be performed on them. Learn more about stacks, by clicking here.

But our today's main focus is on learning the Push and Pop operations in Stacks thoroughly.

Visualising Stacks !

Stacks are just like these plates placed on the top of other, if we want to add more plates we'll keep them on the top of this stack, and if we want to remove some plates, we'll simply pickup plates from the top, notice that we cannot simply take out a plate from the middle or from the end, the movement is bounded, that means we can carry the addition/deletion operation only from one side.

Push Operation :

It refers to adding elements to the stack, this is known as Pushing. A function push() is created which adds elements in the stack. And also helps in checking whether the stack is completely filled or not.

Array Implementation 

We mainly use 2 or 2 variables in the whole Stack play, which are such that, they represent top index of array ,i.e the address of the plate placed at the top. And the base address of the array, i.e the address of the 1st plate near the surface on which it is kept. and a variable to hold the data we want to enter to the stack. Also note, we do not need any variable to hold the address or value of the element that we want to delete, this is because in stacks only the element at the top will be removed. Or we can say the plate we kept at the last turn, would be removed first.

Algorithm :  We send 2 arguments i.e the base address the stack( which is an array in this case) and the data/element we need to enter in the stack. The function will first check whether the array is full or not.
If it is full it will return with a statement "STACK OVERFLOW" otherwise, it will increment the value of TOP by 1 and store the data/element at that location. Thus fulfilling our requirement.

PUSH (STACK, DATA){

If TOP=MAX_SIZE_OF_STACK, then Write OVERFLOW and Exit
else
{
  TOP = TOP + 1
  STACK [TOP] = DATA
  Exit 
}
  
}


Program : 

#include<stdio.h>

#define SIZE 10

int Stack[SIZE], top=-1;

int push(int data)
{
  if (top == SIZE)
  {
    printf("OVERFLOW");
    return -1;
  }
  printf("%d is being pushed to the stack\n",data);
  Stack[++top] = data; 
  printf("Top is now at %d\n", top);
}

int main()
{
  
  push(1);
  push(2);
  push(3);
  push(4);
  printf("The Stack after all the Pushing Operations is :\n");
  for(int i=0;i<=top;i++)
  {
      printf("%d ",Stack[i]);
  }
  return 0;
}


OUTPUT

1 is being pushed to the stack
Top is now at 0
2 is being pushed to the stack
Top is now at 1
3 is being pushed to the stack
Top is now at 2
4 is being pushed to the stack
Top is now at 3
The Stack after all the Pushing Operations is :
1 2 3 4 

Here we declared the array Stack[] and its size, and the variable top as Global variables. So that they could be accessed by any function. Variable top is initially initialised with -1 so that like arrays, its first element also have starting index as 0. And on pushing each value we are also printing the value of top, so as to tell you, how top increases each time.

Linked List Implementation

For learning about this, by clicking here.

Pop Operation :

It refers to the operation which involves deleting the elements of stack one by one, start from the top. Also note there is no way to delete any element from any other position except the top in stacks. We have to delete elements one by one.

Array Implementation 

Here also same variables are required, in order perform the operation. But here we'll remove the element at the top. and after removing one element, another element would become the top element. Then as we will remove elements the value of the variable top will keep on decreasing.
In the function Pop(), we need not pass any arguments, because our character array Stack and the variable top would be global.
* Before perform Pop() operation, ensure that there are some values in the stack, otherwise in the first run, the program will end. So in the below example we are considering that the stack initially has some data.

Algorithm :  Firstly in the function , we will check whether the stack is empty or not, if it is empty, we'll return with statement "STACK UNDERFLOW", else we'll just decrement the value of top, hence the value at the at the previous top(variable) will go out of the scope and hence would be neglected or considered as deleted. We can also store the initial top value in some variable. To print that this value has been deleted, after deleting it. 

If TOP = 0, then Write UNDERFLOW and Exit
ITEM = STACK[TOP]
TOP = TOP-1
Return Item
Exit

Program : 

#include<stdio.h>

#define SIZE 10

int Stack[SIZE], top=-1;

int push(int item)
{
  if (top == SIZE)
  {
    printf("OVERFLOW");
    return -1;
  }
  printf("%d pushed to stack\n",item);
  Stack[++top] = item;
  printf("Top is now at %d\n", top);
}

int pop()
{
  int temp;
  if (top == -1)
  {
    printf("UNDERFLOW \n");
    return -1;
  }
  temp=Stack[top--];
  printf("%d popped from stack\n", temp);
  printf("Top is now at %d\n", top);
  return temp;
}

int main()
{
  int temp;
  push(12);
  push(23);
  temp=pop();
  push(54);
  temp=pop();
  temp=pop();
  temp=pop();
  return 0;
}

OUTPUT

12 pushed to stack
Top is now at 0
23 pushed to stack
Top is now at 1
23 popped from stack
Top is now at 0
54 pushed to stack
Top is now at 1
54 popped from stack
Top is now at 0
12 popped from stack
Top is now at -1
UNDERFLOW

Linked List Implementation

For learning about this, please click here.


Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post