What Is a Stack?
A Stack is a linear data structure that follows the Last In, First Out (LIFO) principle.
LIFO last item added to the stack is the first item removed from it.
Think about a stack of plates in a kitchen. When you place a new plate on top, it becomes the first plate you remove later. You cannot easily take a plate from the middle or bottom without removing the plates above it first.
The Stack (the pile of plates)
You start with 4 plates already stacked, bottom to top:
- Plate 1 — went in first (bottom)
- Plate 2
- Plate 3
- Plate 4 — went in last (top)
Push — adding a plate
A new blue plate is about to be placed on top. The arrow points downward, meaning it lands on the very top of the pile. You can never add a plate to the middle or bottom — only the top.
Pop — removing a plate
Plate 4 is flying off upward (shown faded on the right). The arrow points upward, meaning it's being removed from the top. Again — you can only take from the top, not the middle or bottom.
The golden rule — LIFO
The label at the bottom says it all:
Last In, First Out
Plate 4 was the last one added → it's the first one removed.
Plate 1 was the first one added → it's the last one removed.
Application of Stacks
• String Reverse
• Page visited history in Web browser.
• Undo sequence of text editor.
• Recursive function calling.
⚙️ Three Core Operations
A stack has just three main things it can do:
1. 🔼 Push — Add an item to the top
Stack before: [ 27, 14, 3, 92, 64 ] ← Top is 27
Push(49)
Stack after: [ 49, 27, 14, 3, 92, 64 ] ← Top is now 49
2. 🔽 Pop — Remove the item from the top
Stack before: [ 49, 27, 14, 3, 92, 64 ] ← Top is 49
Pop() → returns 49
Stack after: [ 27, 14, 3, 92, 64 ] ← Top is now 27
- 👀 Peek — Look at the top item WITHOUT removing it Stack: [ 49, 27, 14, 3, 92, 64 ] Peek() → returns 49 Stack: [ 49, 27, 14, 3, 92, 64 ] ← Nothing changed!
💻 Let's Build a Stack in Java
Here's a simple Stack class built using an array:
class StackX {
private int maxSize; // maximum number of items
private double[] stackArray; // the array holding items
private int top; // index of the top item
// Constructor — creates an empty stack
public StackX(int size) {
maxSize = size;
stackArray = new double[maxSize];
top = -1; // -1 means the stack is empty
}
// Push — add item to top
public void push(double item) {
if (top == maxSize - 1) {
System.out.println("Stack is full! Can't push.");
} else {
stackArray[++top] = item; // increment top, then insert
}
}
// Pop — remove item from top
public double pop() {
if (top == -1) {
System.out.println("Stack is empty!");
return -99; // error value
} else {
return stackArray[top--]; // return item, then decrement top
}
}
// Peek — look at top item without removing
public double peek() {
if (top == -1) {
System.out.println("Stack is empty!");
return -99;
} else {
return stackArray[top]; // just return, don't change top
}
}
// isEmpty — returns true if stack has no items
public boolean isEmpty() {
return (top == -1);
}
// isFull — returns true if stack has no room left
public boolean isFull() {
return (top == maxSize - 1);
}
}
🧪 Using the Stack
class StackApp {
public static void main(String[] args) {
StackX myStack = new StackX(10); // stack with max size 10
// Push items onto the stack
myStack.push(30);
myStack.push(80);
myStack.push(100);
myStack.push(25);
// Pop all items and print them
while (!myStack.isEmpty()) {
double val = myStack.pop();
System.out.print(val + " ");
}
// Output: 25.0 100.0 80.0 30.0
}
}
🧩 Let's Trace Through an Example
🚀 Where Are Stacks Used in Real Programming?
Stacks are everywhere, even if you can't see them:
- Function calls — Every time your code calls a function, the computer pushes the return address and arguments onto a stack. When the function finishes, they're popped off.
- Recursion — Recursive functions use the call stack behind the scenes. Expression evaluation — Compilers use stacks to evaluate math expressions.
- Backtracking algorithms — Think maze solvers or game AI.
📝Here is quick Summary
- Stack - A data structure where you can only access the top item
- LIFO - Last In, First Out — last added is first removed
- Push - Add an item to the top
- Pop - Remove the top item
- Peek - Read the top item without removing it
- isEmpty() - Check if there's nothing in the stack
- isFull() - Check if the stack has no room left
If this helped you understand stacks, drop a ❤️ and follow along — next up we'll explore Queues, where the rules are completely different!
Happy coding! 🚀





Top comments (0)