Difference Between Stack and Queue Data Structures (original) (raw)

Last Updated : 6 Jun, 2026

Stack and Queue are linear data structures used to store and manage data efficiently. They differ in the way elements are inserted and removed, making them suitable for different programming scenarios and applications.

Stacks

A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. It can be visualized as a pile of plates where you can only add or remove the top plate.

Operations on Stack:

The primary operations associated with a stack are:

Use Cases of Stack:

**Example:

Java `

import java.util.Stack;

public class StackExample { public static void main(String[] args) {

    // Creating a Stack
    Stack<String> stack = new Stack<>();

    // Pushing elements into Stack
    stack.push("Plate A");
    stack.push("Plate B");
    stack.push("Plate C");

    // Display Stack
    System.out.println("Stack: " + stack);

    // Removing elements (LIFO order)
    System.out.println("Removed: " + stack.pop());
    System.out.println("Removed: " + stack.pop());

    // Display remaining Stack
    System.out.println("Final Stack: " + stack);

    // Peek top element
    System.out.println("Top Element: " + stack.peek());
}

}

`

Output

Stack: [Plate A, Plate B, Plate C] Removed: Plate C Removed: Plate B Final Stack: [Plate A] Top Element: Plate A

**Explanation:

Queues

A queue is a linear data structure that follows the First In, First Out (FIFO) principle. This means that the first element added to the queue is the first one to be removed. It can be visualized as a line of people waiting for a service, where the first person in line is the first to be served.

Operations on Queue

The primary operations associated with a queue are:

Use Cases of Queue:

Queues are used in various applications, including:

**Example:

Java `

import java.util.LinkedList; import java.util.Queue;

public class QueueExample { public static void main(String[] args) {

    // Creating a Queue
    Queue<String> queue = new LinkedList<>();

    // Adding elements into Queue (Enqueue)
    queue.add("Customer A");
    queue.add("Customer B");
    queue.add("Customer C");

    // Display Queue
    System.out.println("Queue: " + queue);

    // Removing elements (Dequeue - FIFO order)
    System.out.println("Removed: " + queue.remove());
    System.out.println("Removed: " + queue.remove());

    // Display remaining Queue
    System.out.println("Final Queue: " + queue);

    // Peek front element
    System.out.println("Front Element: " + queue.peek());
}

}

`

Output

Queue: [Customer A, Customer B, Customer C] Removed: Customer A Removed: Customer B Final Queue: [Customer C] Front Element: Customer C

**Explanation:

Stack vs Queue

Feature Stack Queue
**Definition A linear data structure that follows the Last In First Out (LIFO) principle. A linear data structure that follows the First In First Out (FIFO) principle.
**Primary Operations Push (add an item), Pop (remove an item), Peek (view the top item) Enqueue (add an item), Dequeue (remove an item), Front (view the first item), Rear (view the last item)
**Insertion/Removal Elements are added and removed from the same end (the top). Elements are added at the rear and removed from the front.
**Use Cases Function call management (call stack), expression evaluation and syntax parsing, undo mechanisms in text editors. Scheduling processes in operating systems, managing requests in a printer queue, breadth-first search in graphs.
**Examples Browser history (back button), reversing a word. Customer service lines, CPU task scheduling.
**Real-World Analogy A stack of plates: you add and remove plates from the top. A queue at a ticket counter: the first person in line is the first to be served.
**Complexity (Amortized) **Push: O(1), **Pop: O(1), Peek: O(1) **Enqueue: O(1), **Dequeue: O(1), **Front: O(1), **Rear: O(1)
**Memory Structure Typically uses a contiguous block of memory or linked list. Typically uses a circular buffer or linked list.
**Implementation Can be implemented using arrays or linked lists. Can be implemented using arrays, linked lists, or circular buffers.