Queue vs Stack in Java

1. Introduction

In Java, Queue and Stack are two distinct data structures used for storing and managing a collection of elements. A Queue typically follows the FIFO (First-In-First-Out) principle, meaning the first element added to the queue will be the first one to be removed. Conversely, a Stack follows the LIFO (Last-In-First-Out) principle, where the last element added is the first to be removed.

2. Key Points

1. Queue operates on a FIFO basis, which is like a line at a grocery store.

2. Stack operates on a LIFO basis, much like a stack of plates.

3. Queue is an interface in Java and can be implemented using various classes like LinkedList or PriorityQueue.

4. Stack is a class that extends Vector and provides methods for LIFO access, but it is considered to be a legacy class in Java.

3. Differences

Queue Stack
FIFO: First-In-First-Out LIFO: Last-In-First-Out
Implemented using classes like LinkedList A class that extends Vector
Used for operations where order needs to be preserved Used for operations where reversal of order is needed

4. Example


// Importing necessary classes
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class DataStructureComparison {
    public static void main(String[] args) {
        // Step 1: Create a Queue using LinkedList
        Queue<Integer> queue = new LinkedList<>();

        // Step 2: Create a Stack
        Stack<Integer> stack = new Stack<>();

        // Step 3: Add elements to the Queue
        for(int i = 1; i <= 3; i++) {
            queue.add(i);
        }

        // Step 4: Add elements to the Stack
        for(int i = 1; i <= 3; i++) {
            stack.push(i);
        }

        // Step 5: Print the head of the Queue and the top of the Stack
        System.out.println("Head of Queue: " + queue.peek());
        System.out.println("Top of Stack: " + stack.peek());

        // Step 6: Remove elements from the Queue and Stack
        queue.remove();
        stack.pop();

        // Step 7: Print the elements after removal
        System.out.println("Elements in Queue after removal: " + queue);
        System.out.println("Elements in Stack after removal: " + stack);
    }
}

Output:

Head of Queue: 1
Top of Stack: 3
Elements in Queue after removal: [2, 3]
Elements in Stack after removal: [1, 2]

Explanation:

1. A Queue is implemented using a LinkedList, and elements are added to it.

2. A Stack is instantiated, and the same elements are added to it.

3. The first element of the Queue (the head) and the last element added to the Stack (the top) are displayed.

4. An element is removed from both the Queue and the Stack, showing the FIFO nature of the Queue and the LIFO nature of the Stack.

5. After removal, the Queue no longer contains the first element, and the Stack no longer contains the last element that was added.

5. When to use?

- Use a Queue when you need to manage elements in a first-come-first-serve fashion, like scheduling tasks, managing operations in a service-oriented architecture, or breadth-first search algorithms.

- Use a Stack for managing tasks in a last-come-first-serve manner, which is useful for depth-first search algorithms, parsing expressions, backtracking algorithms, and maintaining function calls (call stack).

Comments