15
[0]
42
[1]
88
[2]
TOPStack initialized with initial elements
Capacity:
3 / 6
Time:
O(1)Shortcuts: P Push • O Pop • K Peek • C Clear • M Sound • Z Focus
Implementation Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Stack<T> {
private items: T[] = [];
push(element: T): void {
this.items.push(element);
}
pop(): T | undefined {
return this.items.pop();
}
peek(): T | undefined {
return this.items[this.items.length - 1];
}
isEmpty(): boolean {
return this.items.length === 0;
}
size(): number {
return this.items.length;
}
clear(): void {
this.items = [];
}
}Complexity Analysis
Access
O(n)
Search
O(n)
Insertion
O(1)
Deletion
O(1)
Space Complexity
O(n)
Color Legend
Primary Node (Top / Front / Head)
Inspected / Peeked
Element / Node
A Stack is a linear data structure that follows the Last In, First Out (LIFO) principle. Elements can only be added or removed from the top of the stack. Stacks are fundamental in computer science, powering execution call stacks, undo/redo mechanisms, syntax parsing, and expression evaluation.