Stack Data Structure
A Stack is a linear data structure that follows the Last In, First Out (LIFO) principle. The last element added to the stack will be the first one to be removed just like a stack of plates in your kitchen!
In programming, stacks are widely used for:
- Reversing data
- Recursion handling
- Expression evaluation
- Syntax parsing
- Backtracking algorithms
- Browser history navigation
Stack Terminology
Understanding the basic terms will help you communicate clearly when designing or implementing algorithms.
- Top: The most recently added element of the stack.
- Push: The action of adding an element to the top of the stack.
- Pop: The action of removing the top element.
- Underflow: Trying to
pop()
from an empty stack. - Overflow: Trying to
push()
onto a full stack (mainly in fixed-size stack implementations). - LIFO (Last In, First Out): The order principle the stack follows.
- Stack Pointer: A variable that typically holds the location of the
top
element.
Working of Stack
Stack chips away at the LIFO design. As we can see in the underneath figure there are five memory blocks in the stack; along these lines, the size of the stack is 5.
Assume we need to store the components in a stack and how about we expect that stack is vacant. We have taken the pile of size 5 as appeared underneath in which we are pushing the components individually until the stack turns out to be full.

Since our stack is full as the size of the stack is 5. In the above cases, we can see that it goes from the top to the base when we were entering the new component in the stack.
The stack gets topped off from the base to the top. At the point when we play out the erase procedure on the stack, there is just a single route for passage and exit as the opposite end is shut.
It follows the LIFO design, which implies that the worth entered first will be eliminated last. In the above case, the worth 5 is entered first, so it will be taken out simply after the cancellation of the multitude of different components.
Standard Stack Operations
Coming up next are some basic activities actualised on the stack:
- push(): When we embed a component in a stack then the activity is known as a push. On the off chance that the stack is full, at that point the flood condition happens.
- pop(): When we erase a component from the stack, the activity is known as a pop. In the event that the stack is unfilled implies that no component exists in the stack, this state is known as an undercurrent state.
- isEmpty(): It decides if the stack is unfilled or not.
- isFull(): It decides if the stack is full or not.'
- peek(): It restores the component at the given position.
- display(): It prints all the components accessible in the stack.
Real World Examples of Stack
- Undo/Redo in text editors.
- Browser History (Back and Forward navigation).
- Recursive function call management in programming languages.
- Parentheses matching and syntax parsing.
A stack might seem simple, but its applications are everywhere from managing function calls to solving some of the most complex problems in computer science.
Whether you're preparing for interviews or solving competitive coding challenges, mastering the stack is a must.