Stack Implementation using Array and Linked List
Introduction
Stack implementation is a crucial topic in Data Structures and Algorithms (DSA) and is frequently asked in coding interviews. If you are learning through a DSA course in Jaipur, understanding how to implement a stack using different data structures will strengthen your core programming skills.
In this lesson, you will learn how to implement a stack using:
- Array
- Linked List
Stack Implementation Overview
A stack follows the LIFO (Last In, First Out) principle. Regardless of implementation, the operations remain the same:
- Push
- Pop
- Peek
- isEmpty
The difference lies in how memory is managed.
1. Stack Implementation using Array
In this approach, a stack is implemented using an array with a fixed size.
How It Works
- Maintain a variable called top
- Initially, top = -1
- Push operation increments top
- Pop operation decrements top
Example
Push elements: 10, 20, 30
Stack → [10, 20, 30]
Time Complexity
- Push → O(1)
- Pop → O(1)
- Peek → O(1)
Advantages
- Easy to implement
- Faster due to contiguous memory
Disadvantages
- Fixed size limitation
- Risk of stack overflow
2. Stack Implementation using Linked List
In this approach, each element is stored as a node in a linked list.
How It Works
- Use head node as top
- Push adds node at beginning
- Pop removes node from beginning
Example
Stack → 30 → 20 → 10
Time Complexity
- Push → O(1)
- Pop → O(1)
- Peek → O(1)
Advantages
- Dynamic size
- No overflow issue (until memory is full)
Disadvantages
- Extra memory for pointers
- Slightly complex implementation
Time Complexity Comparison
O(1)
All stack operations in both implementations take constant time.
Array vs Linked List Implementation
- Array uses fixed memory
- Linked list uses dynamic memory
- Array is faster
- Linked list is more flexible
When to Use Which?
Use Array when:
- Size is known
- Memory usage is predictable
Use Linked List when:
- Size is dynamic
- Flexibility is required
Common Interview Questions
- Implement stack using array
- Implement stack using linked list
- Implement stack using two queues
- Implement two stacks in one array
Real-World Applications
- Function call stack
- Undo/Redo operations
- Expression evaluation
- Backtracking algorithms
Best Practices
- Always check for overflow and underflow
- Use dynamic structures when needed
- Write clean and optimized code
Summary
- Stack can be implemented using array or linked list
- Both support O(1) operations
- Array is simple but fixed
- Linked list is flexible but uses extra memory
FAQs
Q1. Which stack implementation is better?
Both are good; array is faster, linked list is more flexible.
Q2. Why does array stack have overflow?
Because it has a fixed size.
Q3. Does linked list stack overflow?
Only when system memory is full.
Q4. What is the time complexity of stack operations?
All major operations take O(1).
Q5. Is stack implementation important for interviews?
Yes, it is a commonly asked topic.
Internal Link
To explore more programming and development courses, click here for more free courses.



