DS – Stack Data Structure

Stack:

  • Stack is a linear data structure.
  • Stack follows LIFO (Last in First Out).
  • Elements added from one end called TOP of stack and removes from the same end.

Note: We create Stack using arrays, only the way of representation and Rules of accessing elements vary.

ArrayStack
Linear data structureLinear data structure
Process elements using IndexProcess element using TOP
Insert element any whereInsert at top only
Delete element from any whereDelete from top
Display element in any formatDisplay elements in LIFO

Work flow:

Stack ADT: Abstract Data Type specifies the operations can be performed on Stack

  1. Push: Push an element on to the Stack. Returns “Overflow” if stack is full.
  2. Pop: Deletes top item from Stack. Returns “Underflow” if Stack is empty.
  3. Peek: Returns top element of stack but not removes.           
  4. isEmpty: Returns true if stack is empty, else false
  5. isFull: Returns true if stack is full, else false
  6. Traverse: Display elements of Stack.

We can create the Stack in two ways:

  1. Static Stack: Stack with fixed size and can be created using Arrays.
  2. Dynamic Stack: Stack modifies dynamically and can be created using Pointers.

Stack using Arrays:

  • Create an array with name Stack.
  • We can specify the size using Macro(Constant variable).

Global variables: Define Array variable and top as global so that we can access from all functions

Scroll to Top