Stack

Stack is an LIFO (Last In First Out) data structure.

Stack is a linear data structure where insertion and deletion operations are performed from one end called TOP. A TOP is a pointer which points to the top element- the element which entered last in the stack.

Stack can be implemented with an array and a linked list. Here, We will see the implementation of stack using array in C with basic operations on stack.

Basic Operations on Stack

  • PUSH
  • POP

Insertion of element in the stack is called as PUSH and Deletion of element from the stack is called POP operation.

Algorithm:

PUSH(Stack, N, Item)

This algorithm will insert Item at top to Stack having size N. Here initial index for stack is 0.

           if TOP >= N - 1 then
                     "Overflow"
           else
                     TOP = TOP + 1
                     Stack[TOP] = Item
PUSH - Stack using array in C

PUSH Operation on Stack

POP(Stack, N)

This algorithm will delete the top element from the Stack having size N. Here initial index for stack is 0.

  if TOP = -1 then   
           "Underflow"
  else
            Stack[TOP] = NULL
            TOP = TOP - 1
POP - Stack using array in C

POP Operation on Stack

Implementation of Stack using array in C program with PUSH and POP operations :

Copy to Clipboard

Output :