Implementation of stack: The stack can be implemented in two ways: using array and using a linked list. The pop and push operations in the array are simpler than the linked list. But dynamic memory allocation is not possible with the array.

Push operation

Push operation is used to insert a new element in the stack.

In case, the array is full, and no new item can be added in the array. This condition is also called an OVERFLOW (STACK_FULL).

Algorithm of the push operation

Step 1: Check whether the stack is full.
Step 2: If the stack is full, it will print the “overflow” message, and the program will terminate.
Step 3: If the stack is not full, the stack-array will increment [top + 1], and a new item will be added to the stack-array.  

Pop operation

Pop operation is used to delete an element in the stack.

In case, the last item is popped, the stack becomes empty. If one tries to delete an item from an empty stack, this condition is also called an UNDERFLOW (STACK_EMPTY).

Algorithm of the pop operation

Peek operation

When the data is received from a particular location in the stack, that operation is called peep operation.

Algorithm of peek operation

Stack program in C language:

Pin It on Pinterest

Share This