Stacks are a last-in-first-out (LIFO) data structure. This means that the last element added to the stack is the first element to be removed. Stacks are commonly used to implement function calls, backtracking algorithms, and expression evaluation.
class Stack {
constructor() {
this.items = [];
}
push(item) {
this.items.push(item);
}
pop() {
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
}
const stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.peek()); // 3
console.log(stack.pop()); // 3
console.log(stack.pop()); // 2
console.log(stack.pop()); // 1
Read More Topics
Architectural Patterns in System Design
Scalability and Performance in System Design
Database Design in System Design
Distributed Systems in System Design
System Integration and APIs in System Design
Cloud Computing in Sestem Design
Containerization and Orchestration in System Design
High Availability and Disaster Recovery
Performance Tuning and Optimization
Case Studies and Real-World Projects
B.Tech 4 YEAR CS/IT PROJECTS And Placement
Weather Forecasting APP in MERN Project
e-commerce website React.js logic code
JavaScript function that could be used to add a product to the shopping cart