VOOZH about

URL: https://dev.to/gihan_benaragama/stacks-the-simple-data-structure-every-developer-should-know-24ia

⇱ Stacks β€” The Simple Data Structure Every Developer Should Know - DEV Community


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:

  1. Plate 1 β€” went in first (bottom)
  2. Plate 2
  3. Plate 3
  4. Plate 4 β€” went in last (top)

πŸ‘ Plate exsample

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

πŸ‘ push

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

πŸ‘ pop

  1. πŸ‘€ 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!

πŸ‘ peek

πŸ’» 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

πŸ‘ exsample

πŸš€ 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! πŸš€