Operations on Stack
Before we jump into operations, let’s quickly recall what a stack is.
A stack is like a pile of plates — you add plates on top and take plates from the top.
That means, the last plate you put is the first one you take out.
This idea is called LIFO (Last In, First Out).
Now, let’s see how a computer performs actions on this “pile of data.”
⚙️ The Four Main Operations on a Stack
Stacks may sound simple, but they have a few key operations that make them powerful and useful.
Let’s go through them one by one — like a friendly guide explaining how the plates move in and out of the pile!
🥇 1. PUSH Operation (Adding an Element)
PUSH means to add a new item on top of the stack.
When you “push” something, it goes to the top and becomes the latest element.
🧩 How it works:
- Check if there’s space in the stack.
- If there is, increase the top pointer (which keeps track of the top item).
- Place the new item at that position.
If the stack is already full, you can’t push any more elements — this is called Stack Overflow.
Example:
If your stack currently has [10, 20, 30] (where 30 is on top)
and you push 40, your stack becomes:
[10, 20, 30, 40]
↑
Top
🥈 2. POP Operation (Removing an Element)
POP means to remove the topmost item from the stack.
You can only remove what’s currently on the top, not anything below it.
🧩 How it works:
- Check if the stack is empty.
- If it’s not, take out the element at the top.
- Move the top pointer down by one position.
If the stack is already empty, you can’t pop anything — that’s called Stack Underflow.
Example:
If your stack has [10, 20, 30, 40]
and you pop one item, the 40 is removed, leaving you with:
[10, 20, 30]
↑
Top
🥉 3. PEEK (or TOP) Operation
Sometimes, you don’t want to remove anything — you just want to see what’s on top.
That’s what the PEEK operation does.
It simply returns the topmost element of the stack without changing anything.
Example:
If your stack has [10, 20, 30],
then PEEK() will show 30.
Your stack stays the same:
[10, 20, 30]
↑
Top
🏅 4. DISPLAY Operation
The DISPLAY operation shows all the elements in the stack — from top to bottom.
It helps you understand what’s currently stored in the stack.
Example:
If your stack has [10, 20, 30, 40],
the display will look like:
Top → 40
30
20
Bottom → 10
This shows the exact order of how data would come out if you keep popping elements.
🧩 Diagram – Stack Operations
Let’s visualize everything we discussed:
Step 1: Empty Stack
(Top = -1)
[ ]
Step 2: PUSH 10
[10]
Top → 10
Step 3: PUSH 20
[10, 20]
↑
Top
Step 4: PUSH 30
[10, 20, 30]
↑
Top
Step 5: POP
[10, 20]
↑
Top
Each PUSH adds one block above the other.
Each POP removes the block at the top.
🧠 Real-Life Analogy
Think of a stack of coins:
- When you place a new coin, it goes on top (PUSH).
- When you take one, you remove the top coin (POP).
- If you peek, you just look at which coin is on top (PEEK).
- And if you display, you show all the coins in order.
That’s exactly how the stack in computers behaves!
⚡ Common Stack Errors
| Error Type | Description |
|---|---|
| Stack Overflow | Happens when you try to PUSH into a full stack. |
| Stack Underflow | Happens when you try to POP from an empty stack. |
Example:
Imagine a plate rack that’s already full — you can’t add another plate (overflow).
If there are no plates left and you still try to remove one — that’s underflow!
💻 Simple Example in Code (C-like Pseudocode)
#define MAX 5
int stack[MAX];
int top = -1;
void push(int value) {
if (top == MAX - 1)
printf("Stack Overflow");
else
stack[++top] = value;
}
int pop() {
if (top == -1)
printf("Stack Underflow");
else
return stack[top--];
}
int peek() {
if (top != -1)
return stack[top];
}
This shows how easy and structured stack operations are once you understand their logic.