Last Updated : 11 Feb, 2025
A stack is a fundamental data structure in computer science that follows the Last In, First Out (LIFO) principle. This means that the last element added to the stack will be the first one to be removed. Stacks are widely used in various applications, such as function call management, undo mechanisms, and parsing expressions.
What is a Stack?Operations Performed on a StackA stack is a linear data structure that allows operations to be performed at one end, called the top. The two primary operations are:
Extreme Conditions in a Stack
- Push: Adds an element to the top of the stack.
- Pop: Removes and returns the top element from the stack.
- Stack Underflow:
- Occurs when you try to perform a pop or peek operation on an empty stack.
- Handling: Check if the stack is empty before performing these operations.
- Stack Overflow:
- Occurs when you try to push an element into a stack that has reached its maximum capacity (in languages or implementations where the stack size is fixed).
- Handling: Check if the stack is full before performing a push operation.
class Stack {
constructor() {
this.items = [];
}
// Push operation
push(element) {
this.items.push(element);
}
// Pop operation
pop() {
if (this.isEmpty()) {
return "Stack is empty";
}
return this.items.pop();
}
// Peek operation
peek() {
if (this.isEmpty()) {
return "Stack is empty";
}
return this.items[this.items.length - 1];
}
// isEmpty operation
isEmpty() {
return this.items.length === 0;
}
// Size operation
size() {
return this.items.length;
}
// Print the stack
print() {
console.log(this.items);
}
}
// Example Usage
const stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
console.log(stack.peek());
console.log(stack.pop());
console.log(stack.size());
console.log(stack.isEmpty());
stack.print();
30 30 2 false [ 10, 20 ]
Time Complexity: All operations in the Stack Class ( Push , Pop, Peek, isEmpty, Size,) have O(1) time complexity. print Stack()
, which is O(n).
Auxiliary Space: O(1) for all operations.
// Node class representing each element in the stack
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
// Stack class using a Linked List
class Stack {
constructor() {
this.top = null;
this.size = 0;
}
// Push operation
push(value) {
const newNode = new Node(value);
newNode.next = this.top;
this.top = newNode;
this.size++;
}
// Pop operation
pop() {
if (this.isEmpty()) {
console.log("Stack is empty!");
return null;
}
const poppedValue = this.top.value;
this.top = this.top.next;
this.size--;
return poppedValue;
}
// Peek operation
peek() {
return this.isEmpty() ? null : this.top.value;
}
// Check if the stack is empty
isEmpty() {
return this.size === 0;
}
// Returns the size of the stack
getSize() {
return this.size;
}
// Print stack elements
printStack() {
let current = this.top;
let stackValues = [];
while (current) {
stackValues.push(current.value);
current = current.next;
}
console.log("Stack:", stackValues.join(" -> "));
}
}
// Example Usage
const stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
stack.printStack();
console.log("Top Element:", stack.peek());
console.log("Popped Element:", stack.pop());
stack.printStack();
Stack: 30 -> 20 -> 10 Top Element: 30 Popped Element: 30 Stack: 20 -> 10
Time Complexity: All operations in the Stack Class ( Push , Pop, Peek, isEmpty, Size,) have O(1) time complexity.
Auxiliary Space : O(1) for all operations
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4