Stack – Data Structure

Stack - Data Structure

Welcome learners! In this post Stack – Data Structure you will briefly know about what is stack, where is is used and what are the operations of stack etc. You can also refer to my previous post Dynamic Programming (DP),Importance of DSA if needed.

Stack – Data Structure Introduction

This is a one way liner data structure which has many applications. It follows LIFO principle. LIFO stands for Last In First Out. This is named as stack because it is similar to the real world stacks, for example a pile of papers or stack of books. LIFO principle is nothing but, we can only remove the element which is last inserted into it. If we want to remove any element, then we need to remove all the elements inserted after it. You can only view the top element of the stack. Generally you can visually imagine stack as vertical. But we can also imagine it horizontally ,not restricted to vertical.

Stack Representation

Stack is represented linearly with collection several elements. It can be accesses using top variable which points to the top of the stack. We can perform any operation on the stack only from the top end. The other end of the stack is not accessible. So you need to insert and delete only from the top end.

Stack operations

There are majorly two operations of stack. They are PUSH and POP. Push refers to inserting element in the stack i.e. pushing your element into the stack. Pop refers to deletion of element from the stack i.e. popping your element out of the stack.
there are three more operations of the stack. They are peek, isEmpty and isFull. You use Peek to know the top element of the stack. There difference between pop and peek is, pop removes the top element and returns it, but peek returns it without deleting it. And you can use isFull method to check if the stack is full and similarly isEmpty to check if the stack is empty.

Stack Application/Problems

  • Infix to postfix conversion
  • postfix to infix
  • Postfix evaluation
  • prefix to postfix
  • Postfix to infix
  • Postfix to prefix
  • Stock span
  • Balanced parenthesis check
  • Towers of Hanoi
  • Syntax analyzer in compiler

You can find many problems and applications of stack

You practice problems of stack at geekforgeeks