Knowledge Guide
HomeDSAFoundations

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

Image
Image

Queue

Image
Image

Basic Operations On Stack

java
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

Image
Image

Basic Operations on Queue

java
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

Image
Image

Space Complexity for Stack and Queue

Both stacks and queues have a space complexity of , where 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.

🤖 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.

🎨 Explain it visually

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.
🤔 Walk me through it (interactive)

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.
🧪 Quiz me & fix my gaps

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.
🧠 Make it stick

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.

📝 My notes