WikiGalaxy

Personalize

Introduction to Queue

A queue is a linear data structure that follows the First In First Out (FIFO) principle. This means that the element added first will be the first one to be removed. Queues are used in scenarios where order needs to be maintained, such as in scheduling tasks, managing requests, and handling resources.

Example 1: Basic Queue Operations


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

public class BasicQueueOperations {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        
        // Adding elements to the queue
        queue.add("First");
        queue.add("Second");
        queue.add("Third");
        
        // Removing an element from the queue
        System.out.println("Removed: " + queue.remove());
        
        // Displaying the front of the queue
        System.out.println("Front: " + queue.peek());
        
        // Displaying all elements in the queue
        System.out.println("Queue: " + queue);
    }
}
    

Explanation: In this example, we demonstrate basic operations of a queue such as adding, removing, and peeking elements. The queue uses LinkedList to store elements.

Example 2: Implementing a Queue Using Arrays


class ArrayQueue {
    private int front, rear, capacity;
    private int[] queue;

    public ArrayQueue(int c) {
        front = rear = 0;
        capacity = c;
        queue = new int[capacity];
    }

    void enqueue(int data) {
        if (capacity == rear) {
            System.out.println("Queue is full");
            return;
        } else {
            queue[rear] = data;
            rear++;
        }
    }

    void dequeue() {
        if (front == rear) {
            System.out.println("Queue is empty");
            return;
        } else {
            for (int i = 0; i < rear - 1; i++) {
                queue[i] = queue[i + 1];
            }
            if (rear < capacity)
                queue[rear] = 0;
            rear--;
        }
    }

    void display() {
        if (front == rear) {
            System.out.println("Queue is Empty");
            return;
        }
        for (int i = front; i < rear; i++) {
            System.out.print(queue[i] + " ");
        }
        System.out.println();
    }
}

public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue q = new ArrayQueue(5);
        q.enqueue(10);
        q.enqueue(20);
        q.enqueue(30);
        q.display();
        q.dequeue();
        q.display();
    }
}
    

Explanation: This example illustrates how to implement a queue using arrays. It includes methods for enqueueing, dequeueing, and displaying elements in the queue.

Example 3: Circular Queue Implementation


class CircularQueue {
    private int[] queue;
    private int front, rear, size, capacity;

    public CircularQueue(int capacity) {
        this.capacity = capacity;
        queue = new int[capacity];
        front = size = 0;
        rear = capacity - 1;
    }

    boolean isFull() {
        return (size == capacity);
    }

    boolean isEmpty() {
        return (size == 0);
    }

    void enqueue(int item) {
        if (isFull()) {
            System.out.println("Queue is full");
            return;
        }
        rear = (rear + 1) % capacity;
        queue[rear] = item;
        size++;
    }

    int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty");
            return Integer.MIN_VALUE;
        }
        int item = queue[front];
        front = (front + 1) % capacity;
        size--;
        return item;
    }

    void displayQueue() {
        if (isEmpty()) {
            System.out.println("Queue is empty");
            return;
        }
        for (int i = front; i != rear; i = (i + 1) % capacity) {
            System.out.print(queue[i] + " ");
        }
        System.out.print(queue[rear]);
        System.out.println();
    }
}

public class CircularQueueDemo {
    public static void main(String[] args) {
        CircularQueue q = new CircularQueue(5);
        q.enqueue(10);
        q.enqueue(20);
        q.enqueue(30);
        q.enqueue(40);
        q.enqueue(50);
        q.displayQueue();
        q.dequeue();
        q.displayQueue();
    }
}
    

Explanation: This example demonstrates a circular queue implementation. A circular queue allows efficient use of space by connecting the end of the queue back to the start.

Example 4: Priority Queue


import java.util.PriorityQueue;

public class PriorityQueueDemo {
    public static void main(String[] args) {
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        
        pq.add(30);
        pq.add(20);
        pq.add(10);
        
        System.out.println("Priority Queue: " + pq);
        
        System.out.println("Removed from Priority Queue: " + pq.poll());
        
        System.out.println("Priority Queue after removal: " + pq);
    }
}
    

Explanation: This example illustrates the use of a priority queue, where elements are processed based on their priority. The smallest element is given the highest priority by default.

Example 5: Queue Using Two Stacks


import java.util.Stack;

class QueueUsingStacks {
    Stack<Integer> stack1;
    Stack<Integer> stack2;

    public QueueUsingStacks() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    void enqueue(int data) {
        while (!stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }
        stack1.push(data);
        while (!stack2.isEmpty()) {
            stack1.push(stack2.pop());
        }
    }

    int dequeue() {
        if (stack1.isEmpty()) {
            System.out.println("Queue is empty");
            return Integer.MIN_VALUE;
        }
        return stack1.pop();
    }

    void display() {
        System.out.println("Queue: " + stack1);
    }
}

public class QueueUsingStacksDemo {
    public static void main(String[] args) {
        QueueUsingStacks q = new QueueUsingStacks();
        q.enqueue(10);
        q.enqueue(20);
        q.enqueue(30);
        q.display();
        q.dequeue();
        q.display();
    }
}
    

Explanation: This example shows how to implement a queue using two stacks. This approach leverages the LIFO nature of stacks to simulate FIFO behavior of queues.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025