Stack Implementation in C/C++ – Basic Algorithms

Stack implementation

Stack(Last-In-First-Out/ LIFO):

Stack is a linear data structure which can be accessed only from one of its end for storing and retrieving data.

Stack Implementation:

Stack can be implemented by using:

  • Arrays(Static, Dynamic).
  • Linked List.

Operation of Stack:

TOP -> Where insertion takes place.

PUSH -> PUSH is a term used to insert element into stack.

POP -> POP is a term used to delete element from stack.

MAX -> Returns the maximum no of elements held by stack.

Stack implementation

Algorithm for PUSH Operation:

1) Check whether stack is already full or not/Overflow.

   TOP=MAX ; Print “Stack is already full”;


2) TOP=TOP+1

3) Insert ITEM at new position of TOP

     Set Stack[TOP]= ITEM

4) Return

Algorithm for POP Operation:

1) Check whether stack is already empty or not/Underflow.

   TOP=NULL ; Print “Stack is already empty”;


2) Assign TOP item to ITEM

     Set ITEM=Stack[TOP]

3) Set TOP=TOP-1;

4) Return


Please enter your comment!
Please enter your name here