Multiple Stacks
📚 MULTIPLE STACKS (Using Arrays)
✨ What are Multiple Stacks?
-
Multiple stacks mean managing two or more stacks inside a single array.
-
Purpose: To save space and share memory better instead of using separate arrays for each stack.
✅ HOW TO IMPLEMENT MULTIPLE STACKS IN ARRAYS?
There are two ways:
Type | Explanation |
---|---|
1. Fixed Division | Divide array into fixed parts for each stack. Each stack gets its own block. Simple but can waste memory. |
2. Dynamic Division | Allow stacks to grow toward each other. More efficient and avoids memory wastage. |
1️⃣ Fixed Division (Simple Method)
Suppose array size = 10:
-
Stack 1 → indexes 0 to 4
-
Stack 2 → indexes 5 to 9
Each stack behaves independently within its section.
Limitations: One stack may overflow even if other has free space.
2️⃣ Dynamic Sharing (Best Method)
-
Stack 1 grows from left to right.
-
Stack 2 grows from right to left.
-
Overflow occurs only if
top1 + 1 == top2
.
C Program two stacks in an array
📚 Implementing More than Two Stacks in an Array
You already know:
✅ 1 array + 2 stacks = can be managed by growing from opposite ends (left to right and right to left).
But if you want more than two stacks (say 3, 4, or k stacks), things become trickier.
✨ There are mainly TWO Ways
Method | Idea | Difficulty | Memory Efficiency |
---|---|---|---|
1. Fixed Partitioning | Divide array into fixed equal parts for each stack. | Easy | Wastes space |
2. Flexible (Dynamic) Partitioning | Use extra arrays (next[] , top[] ) to link free spaces dynamically. | Hard | Very Efficient |
1️⃣ FIXED PARTITIONING (Simple Method)
-
Divide array into k equal parts.
-
Each stack is allocated a fixed region.
-
Manage each stack separately in its region.
Example:
If array size = 100 and 4 stacks:
-
Stack 1: 0-24
-
Stack 2: 25-49
-
Stack 3: 50-74
-
Stack 4: 75-99
Each stack has its own top
pointer initialized to one less than its start index.
✅ Algorithm (Fixed Partition)
For each stack i
:
-
start[i]
→ starting index of stacki
-
end[i]
→ ending index of stacki
-
top[i]
→ initiallystart[i] - 1
PUSH Operation:
-
Check if
top[i] < end[i]
-
If yes, increment
top[i]
and insert element.
POP Operation:
-
Check if
top[i] >= start[i]
-
If yes, pop element and decrement
top[i]
.
📌 Drawbacks of Fixed Partitioning
-
Suppose Stack 1 fills up and Stack 2 is empty: Stack 1 can't use free space of Stack 2.
-
So not memory efficient if number of stacks is large or usage is uneven.
2️⃣ FLEXIBLE PARTITIONING (Advanced Method)
-
Dynamic allocation inside array.
-
Use 3 arrays:
-
arr[]
→ Actual data -
top[]
→ Top index for each stack -
next[]
→ Helps in linking free spaces
-
-
Also maintain a
free
pointer which points to the next free index.
This method is called k Stacks in a Single Array dynamically.
Advanced but very efficient.
🚀 HOW FLEXIBLE PARTITIONING WORKS?
Suppose you have:
-
arr[10]
-
top[k]
-
next[10]
-
free
initially = 0
At every insertion:
-
Use
free
index -
Update
free
usingnext[]
-
Adjust
next[]
to point to previous top of the stack -
Update
top[]
Comments
Post a Comment