Implement the Queue Data Structure in Java

Exercise:

Write a Java Program to implement the queue data structure.

1.Implement Stack

Click Here to View the Solution!
public class Queue {
    int SIZE = 6;
    int items[] = new int[SIZE];
    int front, rear;
    Queue() {
        front = -1;
        rear = -1;
    }

    // check if the queue is full
    boolean isFull() {
        if (front == 0 && rear == SIZE - 1) {
            return true;
        }
        return false;
    }

    // check if the queue is empty
    boolean isEmpty() {
        if (front == -1)
            return true;
        else
            return false;
    }

    // insert elements to the queue
    void enQueue(int element) {

        // if queue is full
        if (isFull()) {
            System.out.println("Queue is full");
        }
        else {
            if (front == -1) {

                // mark front denote first element of queue
                front = 0;
            }
            rear++;

            // insert element at the rear
            items[rear] = element;
            System.out.println("Insert " + element);
        }
    }

        // delete element from the queue
        int deQueue() {
        int element;

        // if queue is empty
        if (isEmpty()) {
            System.out.println("Queue is empty");
            return (-1);
        }
        else {
            // remove element from the front
            element = items[front];

            // if the queue has only one element
            if (front >= rear) {
                front = -1;
                rear = -1;
            }
            else {
                // mark next element as the front
                front++;
            }
            System.out.println( element + " Deleted");
            return (element);
        }
    }

    // display element of the queue
    void display() {
        int i;
        if (isEmpty()) {
            System.out.println("Empty Queue");
        }
        else {
            // display the front of the queue
            System.out.println("\nFront index-> " + front);

            // display element of the queue
            System.out.println("Items -> ");
            for (i = front; i <= rear; i++)
                System.out.print(items[i] + "  ");

            // display the rear of the queue
            System.out.println("\nRear index-> " + rear);
        }
    }
 public static void main(String[] args) {

        // create an object of Queue class
        Queue q = new Queue();

        // try to delete element from the queue
        // currently queue is empty
        // so deletion is not possible
        q.deQueue();

        // insert elements to the queue
        for(int i = 1; i < 6; i ++) {
            q.enQueue(i);
        }

        // 6th element can't be added to queue because queue is full
        q.enQueue(7);
        q.display();

        // deQueue removes element entered first i.e. 1
        q.deQueue();
        q.display();
    }
}
Click Here to View the Output!
Queue is empty
 Insert 1
 Insert 2
 Insert 3
 Insert 4
 Insert 5
 Insert 7
 Front index-> 0
 Items -> 
 1  2  3  4  5  7  
 Rear index-> 5
 1 Deleted
 Front index-> 1
 Items -> 
 2  3  4  5  7  
 Rear index-> 5
Click Here to View the Explanation!
  • A class Queue is created with variables named size, front and rear. An integer type array is created whose length is equal to the value of size variable.
  • The class constructor initially assigns the values of front and rear equal to -1 denoting an empty array.
  • isFull() method checks to see if the array is filled such that front equals to 0 and rear equals to one less than array size.
  • isEmpty() method checks if the array is completely empty such that front equals to -1.
  • enQueue() method inserts elements in the queue. It takes an integer type parameter and first checks if the queue is full. If that’s not the case, then it checks if the front equals to -1 i.e. if the array is empty. If so, front is assigned 0 otherwise, value of rear is incremented, and the element is inserted on rear index in the array.
  • deQueue() method removes elements from the array. The index from which the element is removed corresponds to the value of the front variable. If the array is empty after dequeue(), front and rear are assigned -1. Otherwise, value of front is incremented.
  • .display() method first checks to see if the queue is empty. If not, a for loop from the front of the queue to its rear executes which prints out all the elements in the array.
  • In the main method, an object of Queue is created and then all the methods of Queue class are tested according to which their results show on the screen.

2.Implement Stack using queue interface

Click Here to View the Solution!
import java.util.Queue;
        import java.util.LinkedList;

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

        // Creating Queue using the LinkedList class
        Queue<Integer> numbers = new LinkedList<>();

        // insert element at the rear of the queue
        numbers.offer(11);
        numbers.offer(22);
        numbers.offer(33);
        System.out.println("Queue: " + numbers);

        // delete element from the front of the queue
        int removedNumber = numbers.poll();
        System.out.println("Removed Element: " + removedNumber);
        System.out.println("Queue after deletion: " + numbers);
    }
}
Click Here to View the Output!
Queue: [11, 22, 33]
 Removed Element: 11
 Queue after deletion: [22, 33]
Click Here to View the Explanation!
  • In this program, a built-in Queue interface has been used for queue structure implementation.
  • The Queue interface called numbers has been implemented by the LinkedList class.
  • It can be seen that a generic type <Integer> is used in conjunction with the queue.
  • Then elements are added to the rear of the queue with the help of offer() method.
  • This queue is printed on the screen.
  • Then an element is removed from the front of the queue using a method called poll().
  • This removed element is displayed on the screen and then the updated queue is printed.