Stack (Push/Pop)
Data Structureseasy#12LIFO data structure — push and pop from the top
Ready
Step 1 of 0
Speed
Input Data
Comma-separated numbers (0-999). Min 2 values.
Algorithm Code
1class Stack {2 constructor() { this.items = []; }3 push(item) { this.items.push(item); }4 pop() { return this.items.pop(); }5 peek() { return this.items[this.items.length-1]; }6 isEmpty() { return this.items.length === 0; }7 size() { return this.items.length; }8}
Best
O(1)
Average
O(1)
Worst
O(1)
Space
O(n)
About Stack (Push/Pop)
A Stack is a LIFO (Last In, First Out) data structure. Elements are added (push) and removed (pop) from the same end (top). Used in function call stacks, expression evaluation, undo operations, DFS traversal, and bracket matching.
Time Complexity: Best: O(1), Average: O(1), Worst: O(1). Space: O(n).