Step 1 of 5

20% Complete

Introduction to Stacks

Learn what stacks are and the LIFO principle

What is a Stack?

A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. Think of a stack like a pile of plates — you add plates to the top and remove them from the top.

The stack is empty

In the visualization above, you can see a simple stack with four elements. The top of the stack is at the top of the visualization, and the bottom is at the bottom. When we add a new element, it goes on top. When we remove an element, it comes from the top.

The LIFO Principle

The defining characteristic of a stack is its LIFO (Last-In-First-Out) behavior. This means:

  • The last element added to the stack will be the first element removed.
  • Elements can only be added to or removed from the "top" of the stack.
  • Access to other elements in the stack is restricted — you can't access elements in the middle without first removing the elements on top.

Real-world Stack Examples:

  • A stack of plates or books
  • The browser's back button (browsing history)
  • The "undo" feature in text editors
  • Function call stack in programming

Basic Stack Operations

  • Push: Add an element to the top of the stack.
  • Pop: Remove the top element from the stack.
  • Peek/Top: View the top element without removing it.
  • isEmpty: Check if the stack is empty.
  • Size: Get the number of elements in the stack.

Stack Implementation Overview

There are two common ways to implement a stack:

Array-based Implementation

Uses an array to store elements. The top of the stack is typically the end of the array.

  • Simple to implement
  • Fixed size (in some languages)
  • Potential for stack overflow

Linked List-based Implementation

Uses a linked list where the head of the list represents the top of the stack.

  • Dynamic size
  • No overflow issues
  • Slightly more complex

In this tutorial, we'll focus on the array-based implementation as it's more straightforward and commonly used.

Basic Stack Class Structure
class Stack {
constructor() {
this.items = []; // Initialize empty array to store elements
}
// Core operations will be implemented in the following sections
push(element) { /* Add element to the top */ }
pop() { /* Remove and return the top element */ }
peek() { /* Return the top element without removing it */ }
// Utility methods
isEmpty() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
}

Advantages and Disadvantages

Advantages

  • Simple implementation
  • Efficient operations (O(1) time complexity)
  • Memory efficient with fixed-size arrays
  • Clear, well-defined access pattern

Disadvantages

  • Limited access (can only access the top element)
  • Fixed size in array implementations (potential overflow)
  • No random access to elements
  • Searching requires emptying the stack

Check Your Understanding

Next Steps

Now that you understand what stacks are and their basic principles, let's move on to implementing the core operations of a stack, starting with the push operation.