Awesome Digital Workshop

πŸ“¦ What is a Stack?

Today , let’s understand Stacks and Queues in detail:

πŸ“¦ What is a Stack?
Think of a stack like a stack of plates:

Key Operations:
βœ… push() β†’ Add an item to the top
βœ… pop() β†’ Remove the top item
βœ… peek() β†’ See the top item without removing
βœ… isEmpty() β†’ Check if the stack is empty

Real-Life Examples:

Example in Code (JavaScript):
js
let stack = [];

stack.push(1); // Add 1
stack.push(2); // Add 2
stack.pop(); // Removes 2
console.log(stack); // [1]

πŸŒ€ What is a Queue?
A queue works like a line at the supermarket:

Key Operations:
βœ… enqueue() β†’ Add to the end
βœ… dequeue() β†’ Remove from the front
βœ… peek() β†’ See who’s first
βœ… isEmpty() β†’ Check if queue is empty

Real-Life Examples:

Example in Code (JavaScript):
js
let queue = [];

queue.push(β€œA”); // Enqueue A
queue.push(β€œB”); // Enqueue B
queue.shift(); // Dequeue A
console.log(queue); // [β€œB”]


πŸ”„ Advanced Types


βœ… When to Use

React ❀️ for more

Exit mobile version