Stack
LIFO (Last-In-First-Out) data structure with O(1) push/pop operations. Stack is a fundamental linear data structure where elements are added and removed from the same end (top). It's essential for function calls, expression evaluation, backtracking algorithms, and undo operations in applications.
Visualization
Interactive visualization for Stack
Stack follows LIFO (Last In, First Out) principle
Size: 4 | Top: 1
BOTTOM
Push Operation
Adds element to the top of the stack. O(1) time complexity.
Pop Operation
Removes and returns the top element. O(1) time complexity.
Peek Operation
Returns the top element without removing it. O(1) time.
Interactive visualization with step-by-step execution
Implementation
1class Stack<T> {
2 private items: T[] = [];
3
4 push(element: T): void {
5 this.items.push(element);
6 }
7
8 pop(): T | undefined {
9 return this.items.pop();
10 }
11
12 peek(): T | undefined {
13 return this.items[this.items.length - 1];
14 }
15
16 isEmpty(): boolean {
17 return this.items.length === 0;
18 }
19
20 size(): number {
21 return this.items.length;
22 }
23}Deep Dive
Theoretical Foundation
Stack follows LIFO (Last-In-First-Out) principle where the last element added is the first to be removed. It supports two primary operations: push (add element to top) and pop (remove element from top). Additional operations include peek/top (view top element without removing) and isEmpty (check if stack is empty). Stacks can be implemented using arrays (with size limitations) or linked lists (dynamic size). The stack pointer tracks the top element. Stack overflow occurs when pushing to a full stack, stack underflow when popping from empty stack. Time complexity is O(1) for all operations, space complexity is O(n) where n is the number of elements.
Complexity
Time
O(1)
O(1)
O(1)
Space
O(n)
Applications
Industry Use
Function call stack in programming languages
Undo/Redo functionality in text editors and IDEs
Expression evaluation and syntax parsing
Browser history navigation (back button)
Backtracking algorithms (maze solving, N-Queens)
Memory management in compilers
Balanced parentheses checking
Depth-First Search (DFS) traversal
Use Cases
Related Algorithms
Binary Search Tree (BST)
A hierarchical data structure where each node has at most two children, maintaining the property that all values in the left subtree are less than the node's value, and all values in the right subtree are greater. This ordering property enables efficient O(log n) operations on average for search, insert, and delete. BSTs form the foundation for many advanced tree structures and are fundamental in computer science.
Queue
FIFO (First-In-First-Out) data structure with O(1) enqueue/dequeue operations. Queue is a fundamental linear data structure where elements are added at one end (rear) and removed from the other end (front). Essential for breadth-first search, task scheduling, and buffering systems.
Hash Table (Hash Map)
A data structure that implements an associative array abstract data type, mapping keys to values using a hash function. Hash tables provide O(1) average-case time complexity for insertions, deletions, and lookups, making them one of the most efficient data structures for key-value storage. The hash function computes an index into an array of buckets from which the desired value can be found.
Heap (Priority Queue)
A complete binary tree data structure that satisfies the heap property: in a max heap, parent nodes are greater than or equal to children; in a min heap, parents are less than or equal to children. Heaps provide O(1) access to the maximum/minimum element and O(log n) insertion and deletion. They're typically implemented as arrays for efficiency and are the foundation of heap sort and priority queues.