SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Singly Linked ListReverse Linked ListDoubly Linked ListData-Structure and Linked-List Trade-offsMerge Two Sorted ListsRemove Nth Node From End of ListStackValid ParenthesesMin StackDaily TemperaturesQueueImplement Queue Using StacksCircular QueuesCircular Queues QuizDesign Circular QueueSliding Window MaximumQueues, Circular Buffers, and Monotonic Deques
Linked Lists, Stacks & Queues

Stack

Understand the LIFO principle, implement a stack, and explore common stack applications.

A stack is a linear data structure that follows LIFO: Last In, First Out. The last item added is the first item removed.

Think of a stack of plates. A clean plate is placed on top, and the next plate is also removed from the top. Reaching a plate in the middle requires removing every plate above it first.

One Point of Access

A stack adds, removes, and reads items at one end, called the top.


How LIFO Works

Start with an empty stack. In the table, the rightmost value is the top.

OperationStack StateWhat Happened
push(10)[10]10 is the only item.
push(20)[10, 20]20 becomes the top item.
push(30)[10, 20, 30]30 is placed above 20.
pop()[10, 20]30, the last item added, is removed first.

Pushing 30 places it above 20 and 10. Popping removes 30 from the same end.

The order of insertion is 10 and 20, then 30. The order of removal is reversed: 30, 20, 10.

Core Operations

OperationPurpose
PushAdd an item to the top.
PopRemove and return the top item.
PeekRead the top item without removing it.
Is EmptyCheck whether the stack contains any items.

Push and pop define the stack's LIFO behavior. Peek and empty checks make the structure safer and easier to use.


Implementing a Stack with an Array

JavaScript arrays and Python lists already provide operations for adding and removing items at the end, so either can act as a stack without a separate class.

1. Create an Empty Stack

const stack = [];

2. Push Items

push adds each new item to the top, represented by the end of the array.

stack.push("first");  // ["first"]
stack.push("second"); // ["first", "second"]

3. Pop Items

pop removes and returns the most recently added item.

console.log(stack.pop()); // "second"
console.log(stack.pop()); // "first"

The values come out in the reverse of their insertion order, which confirms the LIFO rule.

Complete Array Example

const stack = [];

stack.push(1);
stack.push(2);
stack.push(3);

const topItem = stack[stack.length - 1];
const isEmpty = stack.length === 0;

console.log(topItem);     // 3
console.log(isEmpty);     // false
console.log(stack.pop()); // 3
console.log(stack);       // [1, 2]

Performance

OperationTime in the dynamic-array modelReason
PushO(1)O(1)O(1) amortizedMost pushes write one slot. A resize can copy nnn items.
PopO(1)O(1)O(1) amortizedRemoving the last item avoids shifting the others. Some arrays occasionally shrink.
PeekO(1)O(1)O(1)Read the top slot.
Is emptyO(1)O(1)O(1)Read the stored length.

A stack of nnn values occupies O(n)O(n)O(n) space. Check that the stack contains a value before reading or popping its top. Python raises IndexError for an empty pop, while JavaScript's pop() returns undefined.


Applications of Stacks

Function Call Management

When a function is called, its execution context—including local variables and the return address—is pushed onto the call stack. When the function finishes, that context is popped so execution can resume at the correct point. The same process helps manage recursive calls.

Undo Mechanisms

Applications such as text editors push each action onto a stack. Undo pops the most recent action so changes are reversed in the opposite order from which they occurred.

  1. Perform an action and push it.
  2. Choose undo and pop the latest action.
  3. Reverse that action.

Browser Page History

Each visited page can be pushed onto a history stack. Going back removes the current page and reveals the previously visited page.

Other Uses

Stacks are also useful for reversing data and parsing expressions because both tasks need the most recently stored item first.

Key Takeaways

  • A stack follows Last In, First Out order.
  • All interaction happens at the top of the stack.
  • push adds, pop removes, peek reads, and isEmpty checks the stack.
  • Arrays provide a direct stack implementation through end-based operations.
  • Call stacks, undo systems, and browser history all rely on LIFO ordering.

How is this lesson?

Previous lessonRemove Nth Node From End of ListNext lessonValid Parentheses
Edit on GitHubReport on GitHub
On this page
%

On this page

How LIFO WorksCore OperationsImplementing a Stack with an Array1. Create an Empty Stack2. Push Items3. Pop ItemsComplete Array ExamplePerformanceApplications of StacksFunction Call ManagementUndo MechanismsBrowser Page HistoryOther UsesKey Takeaways