Understand Push and Pop operations in Stack with Algorithms – Examples

A Stack is defined as a special type of data structure where items are inserted from one end called top of stack and items are deleted from the same end. Here, the last item inserted will be on the top of the stack. Since, deletion is done from the same end, last item inserted is the first item to be deleted out from the Stack and so, Stack is also called Last In First Out (LIFO) data structure.

Operations of Stack

The various operations that can be performed on Stacks are –

  1. Insert an item into the stack
  2. Delete an item from the stack
  3. Display the content of the stack

From the above definition, it is clear that stack is a collection of similar type of items and naturally, we can use an Array to hold the items of stack. Since array is used, its size is fixed. So, let us assume that 5 items 30, 20, 25, 10 and 40 are to be placed on the stack. The items can be inserted one by one as shown in following figure;

Demonstration of Push operation in Stack

Demonstration of Push operation in Stack

It is clear from the figure that initially stack is empty and top points to bottom of stack. As the items are inserted top pointer is incremented and it points to the topmost item. Here, the items 30, 20, 25, 10, and 40 are inserted one after the other. After inserting 40 the stack is full. In this situation, it is not possible to insert any new item. This situation, it is not possible to insert any new item. This situation is called stack overflow. When an item is to be deleted, it should be deleted from the top as shown in following figure –

Demonstration of Pop operation in Stack

Demonstration of Pop operation in Stack

Since items are inserted from one end, in stack deletion should be done from the same end. So, as the items are deleted, the item below the top items becomes the new top item and so the position of the top most item is decremented as shown in above figure. The items deleted in order are 40, 10, 25, 20 and 30. Finally, when all items are deleted, top points to bottom of stack. When stack is empty, it is not possible to delete any item and the situation is called underflow of stack.

So, main operations to be performed on stacks are insertion and deletion. Inserting an item into the stack when stack is not full is called push operation and deleting an item from the stack when stack is not empty is called pop operation. Other operations that can be performed are display the contents of the stack, check whether the stack is empty or not, etc.

Push/Insert Operation

To design a C Program function, let us assume that three items are already added to stack and stack is identified by s as shown below.

Here, the index top points to 30 which is the topmost item. The value of top is 2. Now, if an item 40 is to be inserted, first increment top by 1 and then insert an item. The corresponding C statement are

top = top + 1; //indicates the top of stack
s[top] = item; //store item to the top of stack

These two statements can also be written as

s[++top] = item; // points top of stack and store item

But, as we insert an item we must take care of the overflow situation, i.e. when top reaches STACK_SIZE – 1, stack results in overflow condition and appropriate error message has to be returned as shown below –

if(top == STACK_SIZE - 1) //when stack is not full

{

printf(“Stack overflow n”);

return;

}

Here, STACK_SIZE should be #defined and is called symbolic constant the value of which cannot be modified. If the above condition fails, the item has to be inserted . Now, the C code to insert an item into the Stack can be written as –

if(top == STACK_SIZE - 1)// when stack in not full

{

printf(“Stack overflow n”);

return;

}

s[++top] = item;// points and stores item on stack

It is clear from this code that as the item is inserted, the contents of the stack identified by S and top are affected and so they should be passed and used as pointers as shown in previous example.

Algorithm for insert/ push operation

Let’s state [STACK_SIZE] in an array of implementing the stack.

Step 1 – [Check for stack overflow]

If top = STACK_SIZE – 1 then

Print overflow and return

Step 2 – top = top + 1

Step 3 – Stack [top] = item

Step 4 – Exit

Delete/ Pop Operation

Deleting an element from the stack is called Pop operation. This can be achieved by first accessing the top element S[top] and then decrementing top by one as shown below –

item = s[top --]; //deletes item from stack and decrease value of top

Each time the item is deleted, top is decremented and finally, when the stack is empty the top will be -1 and so, it is not possible to delete any item form the stack. The above statement has to be executed only if stack is not empty. Hence, the code to delete an item from stack can be written as –

if (top == -1)

{

return -1; //indicates stack is empty

}

/* access the item and delete */

item = S[top --];

return item;

As the value of top changes every time the item is deleted, top can be used as a pointer variable. The complete function is shown in previous example;

Algorithm of Pop/Delete operation

Let S[STACK_SIZE] be an array for implementing the state

Step 1 – if (top == -1) then

Print stack is empty and return

Else

Set item = S[top]

Step 2 – Set top = top – 1

Step 3 – Return the deleted item from the Stack

Step 4 – Exit

So, these two are the operation on stack, we have already posted the code in to C language, you can find it in the link (Click Here). Hope this article was helpful, if you have some problem related then you can any time leave them as comments.

3 thoughts on “Understand Push and Pop operations in Stack with Algorithms – Examples

  1. [...] is a data structure modeled after a line of people waiting to be served. Along with stacks, queues are one of the simplest kinds of data structures. In this respect, a queue is like the line [...]

Leave a Reply