๐ฆ Introduction to Stack, LIFO Principle, Terminologies & Real-World Applications
Welcome to the Stack in Java series!
So far, we have learned several important data structures, including:
- โ Arrays
- โ Singly Linked Lists
- โ Doubly Linked Lists
Now it’s time to learn one of the most important and widely used linear data structures in Computer Scienceโthe Stack.
Stacks are used everywhere!
From your web browser and text editor to the Java Virtual Machine (JVM), recursion, expression evaluation, and even the Undo feature in applicationsโall of these rely on stacks.
In this tutorial, you’ll learn the complete theory behind stacks before implementing them using Arrays and Linked Lists in the next parts.
๐ Table of Contents
- What is a Stack?
- Why Do We Need a Stack?
- History of Stack
- LIFO Principle
- Real-Life Examples
- Basic Terminologies
- Stack Operations
- Stack Representation
- Applications of Stack
- Advantages
- Disadvantages
- Time Complexity
- Frequently Asked Questions
- Conclusion
๐ What is a Stack?
A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle.
This means:
The last element inserted into the stack is the first element removed.
Think of it as a pile of books.
When you place books one on top of another, the last book you place is the first one you can remove.
Example
Top
โ
+------+
| 50 |
+------+
| 40 |
+------+
| 30 |
+------+
| 20 |
+------+
| 10 |
+------+
Bottom
If you remove one element,
the value removed will be
50
๐ค Why Do We Need a Stack?
Imagine you’re using Microsoft Word.
You type
Hello
Then
Hello World
You press Undo.
The latest action disappears first.
Why?
Because the application remembers the most recent operation first.
This is exactly how a Stack works.
Similarly,
when you visit websites
Google
โ
YouTube
โ
Wikipedia
Pressing the Back button takes you to the previous page in reverse order.
Again,
a Stack is being used.
๐ A Brief History of Stack
The stack concept became popular during the early development of programming languages and compilers.
It was introduced to efficiently manage:
- Function calls
- Local variables
- Recursion
- Program execution
Today, almost every programming language internally uses a Call Stack.
๐ฆ Understanding the LIFO Principle
LIFO stands for
Last In, First Out
Let’s understand it using an example.
Insert
10
20
30
40
50
The stack becomes
Top
โ
50
40
30
20
10
Now remove elements.
The removal order will be
50
โ
40
โ
30
โ
20
โ
10
Notice that
the last inserted element (50) was removed first.
๐ Real-Life Examples of Stack
๐ฝ Stack of Plates
One of the best examples.
Imagine plates kept in a restaurant.
Top
๐ฝ
๐ฝ
๐ฝ
๐ฝ
Bottom
The last plate placed is always removed first.
๐ Stack of Books
Book 5
Book 4
Book 3
Book 2
Book 1
To remove Book 1,
you must first remove all books above it.
๐ Parking Garage
Cars parked in a single narrow lane behave like a stack.
The last car parked exits first.
๐ Browser History
Visit pages
Google
โ
OpenAI
โ
GitHub
โ
YouTube
Back button removes
YouTube
โ
GitHub
โ
OpenAI
โ
Google
โ Undo Operation
Typing
A
โ
AB
โ
ABC
โ
ABCD
Undo
ABCD
โ
ABC
โ
AB
โ
A
๐ Basic Terminologies
Before implementing a stack, let’s understand some important terms.
๐ Top
The Top points to the last inserted element.
Example
Top
โ
50
40
30
20
Current Top
50
๐ Bottom
The bottom stores the oldest element.
50
40
30
20
10
Bottom
10
๐ Push
Push means
Insert an element into the stack.
Example
Current
30
20
10
Push
40
Result
40
30
20
10
๐ Pop
Pop means
Remove the top element.
Current
40
30
20
10
Pop
40
Result
30
20
10
๐ Peek (Top)
Peek means
View the top element without removing it.
Current
50
40
30
Peek
50
Stack remains unchanged.
๐ Overflow
Overflow occurs when we try to insert an element into a full stack.
Example
Suppose the stack size is
5
Already contains
10
20
30
40
50
Push
60
Result
Stack Overflow
๐ Underflow
Underflow occurs when we try to remove an element from an empty stack.
Current
Empty Stack
Pop
Result
Stack Underflow
๐ง Basic Stack Operations
Every Stack supports five important operations.
โ Push
Insert an element.
โ Pop
Remove the top element.
๐ Peek
Display the top element.
โ isEmpty()
Checks whether the stack is empty.
Returns
True
or
False
๐ Display
Print all elements.
๐จ Stack Representation
Suppose we push
10
20
30
40
The stack becomes
Top
โ
+------+
| 40 |
+------+
| 30 |
+------+
| 20 |
+------+
| 10 |
+------+
Now perform
Pop()
Result
Top
โ
+------+
| 30 |
+------+
| 20 |
+------+
| 10 |
+------+
๐ Applications of Stack
Stacks are used in many applications.
๐ป Function Calls
Every function call is stored in a Call Stack.
๐ Recursion
Recursive functions use stacks internally.
๐ Browser Back Button
Stores visited webpages.
โ Undo & Redo
Editors use stacks to undo and redo operations.
๐งฎ Expression Evaluation
Stacks evaluate
- Postfix
- Prefix
expressions.
๐ค Parentheses Matching
Compilers use stacks to check
()
{}
[]
๐ณ Depth First Search (DFS)
DFS uses stacks for graph traversal.
๐ฎ Backtracking
Maze solving
Sudoku
Chess engines
AI algorithms
๐พ Java Virtual Machine
Every Java program uses a JVM Stack to manage:
- Method Calls
- Parameters
- Local Variables
- Return Addresses
๐ Advantages of Stack
โ Simple implementation.
โ Fast insertion.
โ Fast deletion.
โ Supports recursion.
โ Efficient memory management.
โ Useful in many real-world applications.
โ Disadvantages
โ Random access is not possible.
โ Only the top element is accessible.
โ Overflow may occur in fixed-size stacks.
โ Searching is inefficient.
๐ Time Complexity
| Operation | Time Complexity |
|---|---|
| Push | O(1) |
| Pop | O(1) |
| Peek | O(1) |
| isEmpty | O(1) |
| Display | O(n) |
| Search | O(n) |
โ๏ธ Stack vs Queue
| Feature | Stack | Queue |
|---|---|---|
| Principle | LIFO | FIFO |
| Insertion | Top | Rear |
| Deletion | Top | Front |
| Example | Plate Stack | Ticket Queue |
๐ฏ Common Interview Questions
Q1. What is a Stack?
A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle.
Q2. What is LIFO?
The last element inserted is the first element removed.
Q3. What are the basic Stack operations?
- Push
- Pop
- Peek
- isEmpty
- Display
Q4. What is Overflow?
Overflow occurs when inserting into a full stack.
Q5. What is Underflow?
Underflow occurs when removing from an empty stack.
Q6. What is the time complexity of Push?
O(1)
Q7. Which real-world applications use stacks?
- Browser History
- Undo/Redo
- Function Calls
- Recursion
- Expression Evaluation
- Parentheses Matching
- DFS
Q8. Can we access the middle element directly?
No.
Stacks allow access only to the top element.
๐ฏ Key Takeaways
- ๐ฆ Stack follows the LIFO principle.
- โ Push inserts an element.
- โ Pop removes the top element.
- ๐ Peek displays the top element without removing it.
- โ Overflow occurs when a full stack receives another element.
- โ Underflow occurs when removing from an empty stack.
- ๐ Push, Pop, and Peek all take O(1) time.
- ๐ Stacks are widely used in compilers, browsers, editors, recursion, and operating systems.
๐ Conclusion
A Stack is one of the simplest yet most powerful data structures in Computer Science. Its LIFO behavior makes it ideal for solving problems where the most recently added item must be processed first. Whether you’re implementing browser history, undo functionality, recursive algorithms, or expression evaluation, stacks play a fundamental role.
Understanding the concepts covered in this part will make it much easier to implement stacks efficiently using different data structures.
๐ In Part 2, we’ll learn how to implement a Stack using Arrays, including:
- ๐ฆ Creating a Stack
- โ Push Operation
- โ Pop Operation
- ๐ Peek Operation
- โ isEmpty()
- โ isFull()
- ๐ Display Operation
- ๐ป Complete Java Program
- ๐ Dry Runs
- ๐ Time Complexity Analysis