Data Structures and Algorithms

Stack & Queue

Stack Fundamentals

Definition: A stack follows the Last In, First Out (LIFO) principle.
Analogy: Like a stack of plates; the last plate added is the first removed.

Stack Operations

Push: Add an element to the top.
Pop: Remove the top element.
Peek: View the top element without removing it.
isEmpty(): Check if the stack is empty.
Size: Number of elements in the stack.
Clear: Remove all elements.

Stack Properties

Allows duplicate values.
Operations are only at the top.
Sequential access required for specific elements.
Sorting within a stack is not possible.

Time Complexity

Push, Pop, Peek: O(1)
isEmpty(), Size: O(1)
Clear: O(N)

Queue Fundamentals

Definition: A queue follows the First In, First Out (FIFO) principle.
Analogy: Like a line at a grocery store; the first person in line is the first served.

Queue Operations

Enqueue: Add an element to the rear.
Dequeue: Remove an element from the front.
Peek: View the front element without removing it.
isEmpty(): Check if the queue is empty.
Size: Number of elements in the queue.
Clear: Remove all elements.

Time Complexity

Enqueue, Dequeue, Peek: O(1)
Clear: O(N)
For more detailed information on Stack & Queue, refer to the Notion link: Stack & Queue
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.