Stack and Queue
Stacks and Queues are fundamental data structures that allow insertion and deletion of elements in a specific order. A Stack follows a Last In, First Out (LIFO) order, where the last element added is the first to be removed. A Queue follows a First In, First Out (FIFO) order, where the first element added is the first to be removed.
Key Characteristics of Stack and Queue
Stack
- LIFO: Last element added is the first to be removed.
- Common operations:
push(add) andpop(remove). - Examples: Call stack in programming, undo mechanisms in text editors.
Queue
- FIFO: First element added is the first to be removed.
- Common operations:
enqueue(add) anddequeue(remove). - Examples: Task scheduling, handling requests in order, printer queue.
Basic Operations On Stack
import java.util.Stack;
public class Solution {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
// Push operation (O(1))
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println("Stack after push operations: " + stack);
// Peek operation (O(1))
System.out.println("Top element (peek): " + stack.peek());
// Pop operation (O(1))
int poppedElement = stack.pop();
System.out.println("Popped element: " + poppedElement);
System.out.println("Stack after pop operation: " + stack);
// Search operation (O(n))
int position = stack.search(20);
if (position != -1) {
System.out.println("Element 20 found at position: " + position);
} else {
System.out.println("Element 20 not found in the stack.");
}
// Check if the stack is empty (O(1))
System.out.println("Is the stack empty? " + stack.isEmpty());
}
}
Time Complexity Analysis for Stack
- Push:
– Adding an element involves updating the top pointer. - Pop:
– Removing the top element only updates the pointer. - Peek:
– Accessing the top element is immediate. - Search:
– Linear search required. - Access by Index:
– Traversal required. - is_empty:
– Direct check.
Basic Operations on Queue
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
// Enqueue operation (O(1))
queue.add(10);
queue.add(20);
queue.add(30);
System.out.println("Queue after enqueue operations: " + queue);
// Front operation (peek) (O(1))
System.out.println("Front element (peek): " + queue.peek());
// Dequeue operation (O(1))
int dequeuedElement = queue.poll();
System.out.println("Dequeued element: " + dequeuedElement);
System.out.println("Queue after dequeue operation: " + queue);
// Search operation (O(n))
boolean found = queue.contains(20);
if (found) {
System.out.println("Element 20 found in the queue.");
} else {
System.out.println("Element 20 not found in the queue.");
}
// Check if the queue is empty (O(1))
System.out.println("Is the queue empty? " + queue.isEmpty());
}
}
Time Complexity Analysis for Queue
- Enqueue:
– Adding an element at the end is straightforward. - Dequeue:
– Removing the front element is constant time. - Front (Peek):
– Accessing the front element without modification is immediate. - Search:
– Linear search is required for finding an element. - Access by Index:
– Traversal needed to reach the element. - is_empty:
– Direct check.
Space Complexity for Stack and Queue
Both stacks and queues have a space complexity of n is the number of elements in the structure. Each element occupies constant space, and the overall space usage grows linearly with the number of elements.
- Space Complexity:
, as each element added to the stack or queue requires additional memory.
🤖 Don't fully get this? Learn it with Claude
Stuck on Stack and Queue? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **Stack and Queue** (DSA) and want to truly understand it. Explain Stack and Queue from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **Stack and Queue** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **Stack and Queue** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **Stack and Queue** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.