Data Structure & Algorithm in Java

⌘K
  1. Home
  2. Docs
  3. Data Structure & Alg...
  4. Queues
  5. Linear Queue

Linear Queue

A linear queue, also known as a simple queue, is a basic implementation of a queue data structure where elements are added at the rear end and removed from the front end.

• It follows the First In, First Out (FIFO) principle, which means that the element that is added first will be the first one to be removed.

• In a linear queue, elements are stored in a linear manner, similar to a line of people waiting for a service.

Screenshot 2024 03 16 181029
  • Enqueue: Adding an element to the rear end of the queue.
  • Dequeue: Removing an element from the front end of the queue.
  • Peek: Viewing the element at the front of the queue without removing it.
  • IsEmpty: Checking if the queue is empty.
  • IsFull: Checking if the queue is full (in the case of a fixed-size queue).
Applications of Linear Queue
public class LinearQueue {
    private int[] array;
    private int front; // Index of the front element
    private int rear; // Index of the rear element
    private int capacity; // Maximum capacity of the queue

    // Constructor to initialize the queue with a given capacity
    public LinearQueue(int capacity) {
        this.capacity = capacity;
        array = new int[capacity];
        front = rear = -1; // Initialize front and rear to -1 to indicate an empty queue
    }

    // Method to check if the queue is empty
    public boolean isEmpty() {
        return front == -1;
    }

    // Method to check if the queue is full
    public boolean isFull() {
        return (rear + 1) % capacity == front;
    }

    // Method to enqueue an element into the queue
    public void enqueue(int element) {
        if (isFull()) {
            System.out.println("Queue is full. Cannot enqueue.");
            return;
        }
        if (isEmpty()) {
            front = 0;
        }
        rear = (rear + 1) % capacity;
        array[rear] = element;
    }

    // Method to dequeue an element from the queue
    public int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty. Cannot dequeue.");
            return -1;
        }
        int removedElement = array[front];
        if (front == rear) {
            front = rear = -1; // Reset front and rear to -1 to indicate an empty queue
        } else {
            front = (front + 1) % capacity;
        }
        return removedElement;
    }

    // Method to get the front element of the queue without removing it
    public int peek() {
        if (isEmpty()) {
            System.out.println("Queue is empty. No element to peek.");
            return -1;
        }
        return array[front];
    }

    public static void main(String[] args) {
        LinearQueue queue = new LinearQueue(5); // Create a queue with capacity 5

        // Enqueue elements into the queue
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);

        // Print the front element of the queue
        System.out.println("Front element: " + queue.peek());

        // Dequeue and print elements from the queue
        System.out.println("Dequeued: " + queue.dequeue());
        System.out.println("Dequeued: " + queue.dequeue());
        System.out.println("Dequeued: " + queue.dequeue());

        // Try dequeuing from an empty queue
        System.out.println("Dequeued: " + queue.dequeue());
    }
}

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *