Stack Data Structure

Yasir
2 min readMay 4, 2020

--

A Short Explained and Clean Code For Stack

Definition: A stack is an ordered list in which insertion and deletion are done at one end, called top. The last element inserted is the first one to be deleted. Hence, it is called the Last in First out (LIFO) or First in Last out (FILO) list.

Special names are given to the two changes that can be made to a stack. When an element is inserted in a stack, the concept is called push, and when an element is removed from the stack, the concept is called pop. Trying to pop out an empty stack is called underflow and trying to push an element in a full stack is called overflow.

Main stack operations

  • Push (int data): Inserts data onto stack.
  • int Pop(): Removes and returns the last inserted element from the stack.

Auxiliary stack operations

  • int Top(): Returns the last inserted element without removing it.
  • int Size(): Returns the number of elements stored in the stack.
  • int IsEmptyStack(): Indicates whether any elements are stored in the stack or not.
  • int IsFullStack(): Indicates whether the stack is full or not.

Direct applications

  • Balancing of symbols
  • Infix-to-postfix conversion
  • Evaluation of postfix expression
  • Implementing function calls (including recursion)
  • Finding of spans (finding spans in stock markets)
  • Page-visited history in a Web browser [Back Buttons]
  • Undo sequence in a text editor
  • Matching Tags in HTML and XML

Ways to Implement It

  • Simple array based implementation
  • Dynamic array based implementation
  • Linked lists implementation

Stack Implementation using Array (C++)

First Half Of the Program
Second Half Of the Program

--

--