What is Stack

  • Stack is a data structure used for storing data in particular order.
  • It follow the Last In First Out(LIFO) pattern for storing the data.
  • LIFO means the data will be deleted first which is inserted at last.
  • There are two variables used for performing operations on stack TOP and MAX.
  • TOP is used for maintaining index where data can be inserted and deleted.
  • MAX is used for the maximum size of Stack.
  • Data can be stored till MAX-1 as indexing start from 0.
There are 3 operation performed in stack.
1) PUSH : It is used to insert element on top of the Stack. Before inserting element check TOP = MAX-1, if yes, then stack is full. In this condition new element cannot be inserted. if no, then increment TOP by 1(TOP=TOP+1) and insert element to new index.
2) POP : It is used to delete element from top of the Stack. Before deleting element check TOP=-1, if yes, then stack is empty. In this condition element cannot be deleted. if no, then delete the TOP element and decrement the TOP by 1 (TOP=TOP-1).
3) PEEK : It is used to return the value of topmost element of stack.

Share Me

Leave a Reply