stack data structure

Stack Data Structure

Last Updated on

Let’s say you have collection of books, and someone ask you to pile them up.

Creating a pile of book

stack data structure

Now, your friend ask you to perform these actions.

Adding one book into pile

You have only one option for this: To place on top of the book pile. Hence, pile size become bigger by 1 book.

Removing a book from pile

You have only one option for this: To remove top most book on the pile. Hence, pile size become lesser by 1 book.

Find top of the pile

You need to check the top most book, and return its title.

Find size of pile

You need to find number of books in this book pile.

What is stack data structure?

Stack data structure is a linear data structure, where insertion and deletion of items take place from single end only. As we were seeing the case of book piles above, they represent stack data structure.

stack data structure implementation

Stack work on the principle of LIFO (Last In, First Out).

Stack operations

  • Push: Add new item in the stack. If the stack is full, then it is said to be an Overflow condition.
  • Pop:Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
  • Peek or Top:Returns top element of stack.
  • isEmpty: Returns true if stack is empty, else false.

stack data structure

 

Stack Implementation Pseudo code

Stacks are used in either using linked list or array. Here, you can check the pseudo code for stack implementation.

Main Module

  • Module main()
  • Declare Integer max_size
  • Declare Integer menuSelection
  • Call createStack()
  • Do
  • Call display (menuSelection)
  • Select menuSelection
  • Case 1:
  • Call Push(item)
  • Case 2:
  • Call Pop(item)
  • Case 3:
  • Call top()
  • Case 4:
  • Call isEmpty()
  • Case 5:
  • Call getSize()
  • Case 6:
  • Call isFull()
  • End Select
  • While menuSelection!=7
  • End Module

createStack Module

  • Module createStack()
  • Constructor()
  • List=new Single Linked list
  • End Constructor
  • End Module

Push Module

  • Module Push(Node newelement)
  • If getSize() < max_size
  • prepend(newelement)
  • End If
  • End Module

Pop Module

  • Module Pop(Node element)
  • If getSize() > 0
  • remove (element)
  • End If
  • End Module

Top Module

  • Module Top()
  • Return list[n-1]
  • End Module

getSize Module

  • Module getSize()
  • Return len(list)
  • End Module

isEmpty Module

  • Module IsEmpty
  • If len(list)=0
  • Return true
  • End if
  • End Module

isFullModule

  • Module IsFull
  • If len(list)=max_size
  • Return true
  • End if
  • End Module

Flowchart of stack implementation

stack data structure flowchart

Stack data structure Interview questions

  • Stack implementation using linked list
  • Stack implementation using array
  • Reverse a String using Stack
  • Evaluation of Postfix Expression
  • Design a stack which return minimum element in constant time
  • Design a stack which return minimum element without using auxiliary stack
  • Infix to Postfix Conversion using Stack
  • Implement two stacks in an array
  • Check for balanced parentheses in an expression
  • Reverse a string using stack without recursion

End Note

In this article, we have shared you stack data structure, implementation of stack data structure and questions that are frequently asked in job interviews and college assignments. Feel free to contact us if you have any doubt or confusion regarding stack data structure. You can contact us on info@xamnation.com regarding your programming assignment help and job interview preparation.

More resources on Data Structure and Programming

Leave a Comment

Your email address will not be published. Required fields are marked *