Stack Operations
In this section, we’ll explain each Stack operation step by step, with real-life comparisons and clear algorithms to help you visualize the logic.
Let’s break down the core operations that define how a stack works:
Operation | Description |
---|---|
push(item) | Adds an element to the top of the stack. |
pop() | Removes and returns the top element from the stack. |
peek() / top() | Returns the top element without removing it. |
isEmpty() | Checks whether the stack is empty. |
size() | Returns the total number of elements in the stack. |
1. PUSH Operation (Insert an Element)
When you "PUSH" an element, you place it on the top of the stack. Think of stacking plates: each new plate goes on the top.
Before You Push
- You must check if the stack has enough space.
- If the stack is full, you cannot push (this is called Overflow).
Algorithm:
if TOP == MAX_SIZE - 1 then
Output "Stack Overflow - No space to push new element."
else
TOP ← TOP + 1 // move the pointer up
STACK[TOP] ← item // place the new element at TOP
end if
Explanation
TOP
is the index of the last element in the stack.MAX_SIZE
is the maximum capacity of the stack.- If
TOP
is equal toMAX_SIZE - 1
, the stack is full. - If there is space, increment
TOP
and place the new item there.
2. POP Operation (Remove the Top Element)
When you "POP" an element, you remove the top element from the stack. Imagine removing the top plate from a pile.
Before You Pop
- You must check if the stack is empty.
- If the stack is empty, popping is impossible (this is called Underflow).
Algorithm
if TOP == -1 then
Output "Stack Underflow - No elements to pop."
else
item ← STACK[TOP] // store the top item
TOP ← TOP - 1 // move the pointer down
return item
end if
Explanation
- When
TOP == -1
, it means there are no elements. - If elements exist, the top one is stored and removed.
- After removal, the
TOP
pointer is moved one step down.
3. PEEK (TOP) Operation (View the Top Element)
PEEK allows you to look at the top element without removing it. Imagine checking which plate is on top without lifting it.
When you only want to see the last inserted item but don't want to disturb the stack.
Algorithm
if TOP == -1 then
Output "Stack is Empty - Nothing to peek."
else
return STACK[TOP]
end if
Explanation
- If
TOP == -1
, the stack is empty. - If not, return the item at the
TOP
position.
4. ISEMPTY Operation (Check if Stack is Empty)
This operation checks if the stack has no elements. Before performing POP
or PEEK
, it’s always a good idea to check whether the stack is empty to avoid errors.
Algorithm
if TOP == -1 then
return TRUE // Stack is empty
else
return FALSE // Stack has at least one element
end if
If the TOP
pointer is -1
, there are no elements in the stack.
5. SIZE Operation (Count Elements in the Stack)
This operation returns the number of elements currently present in the stack.
Algorithm
return TOP + 1
Since the TOP
pointer starts at -1
(meaning empty), the formula TOP + 1
will always give the number of items present.
Summary Table
Operation | Purpose | Key Condition to Check |
---|---|---|
PUSH | Add new item to the top of the stack | Check for Stack Overflow |
POP | Remove the top item from the stack | Check for Stack Underflow |
PEEK/TOP | View the top item (without removing it) | Check if stack is empty |
ISEMPTY | Check if stack has no items | TOP == -1 means empty |
SIZE | Return total number of items in stack | TOP + 1 gives the count |
Stack Implementation in C - All Operations
#include <stdio.h>
#define MAX 5 // maximum size of stack
int stack[MAX]; // array to hold stack elements
int top = -1; // initially the stack is empty
// Function to push an element
void push(int item) {
if (top == MAX - 1) {
printf("Stack Overflow! Cannot push %d\n", item);
} else {
top = top + 1;
stack[top] = item;
printf("%d pushed into the stack.\n", item);
}
}
// Function to pop an element
int pop() {
if (top == -1) {
printf("Stack Underflow! Cannot pop.\n");
return -1;
} else {
int item = stack[top];
top = top - 1;
printf("%d popped from the stack.\n", item);
return item;
}
}
// Function to peek at the top element
int peek() {
if (top == -1) {
printf("Stack is empty!\n");
return -1;
} else {
printf("Top element is: %d\n", stack[top]);
return stack[top];
}
}
// Function to check if the stack is empty
int isEmpty() {
if (top == -1) {
printf("Stack is empty.\n");
return 1;
} else {
printf("Stack is NOT empty.\n");
return 0;
}
}
// Function to get the current size of the stack
int size() {
printf("Current stack size: %d\n", top + 1);
return top + 1;
}
// Main function to test all stack operations
int main() {
push(10);
push(20);
push(30);
peek(); // Should print 30
size(); // Should print 3
pop(); // Should remove 30
peek(); // Should now show 20
isEmpty(); // Should return 0 (false)
pop();
pop();
pop(); // Should give Underflow warning
isEmpty(); // Should return 1 (true)
size(); // Should print 0
return 0;
}
Explanation
push()
adds a new element if the stack is not full.pop()
removes the top element if the stack is not empty.peek()
shows the top element.isEmpty()
checks if there are any elements.size()
returns the number of elements in the stack.