A Stack is a LIFO (Last-In, First-Out) data structure where elements are pushed and popped exclusively from the top pointer.
Advance top pointer from -1 to 0 and insert 10 at top of stack.
Top: 0 | Stack: [10]Advance top pointer from 0 to 1 and place 20 above 10.
Top: 1 | Stack: [10, 20] (Top element is 20)Remove and return element at top pointer (20), then decrement top pointer.
Popped Value: 20 | Top: 0 | Stack: [10]Clean plates are stacked on top of each other. You can only place a new plate on the top (Push) or take the top plate off (Pop).
1 step (Constant)
O(1) Constant Time
O(N) Auxiliary Space
Memory growth rate as N expands.
Relative efficiency rating for large N.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Push | O(1) | O(1) |
| Pop | O(1) | O(1) |
| Peek | O(1) | O(1) |
class Stack {
constructor() { this.items = []; }
push(elem) { this.items.push(elem); }
pop() { return this.items.pop(); }
peek() { return this.items[this.items.length - 1]; }
}Tracks back/forward navigation history.
Tracks active compiler execution frames.
Stores action history in text editors.
Evaluate your understanding of LIFO stack mechanics, push/pop operations, call stack frames, and bracket matching.
Use stacks whenever you need to reverse sequences or evaluate nested structures like code expressions and HTML tags!
Stack initialized with 3 elements.