Queue based on Linked List in Java

Write a program to implement a queue data structure using a linked list with the following operations:

  • Push: To insert an element into the queue.
  • Pop: To remove an element from the queue.
  • Display: To display the contents of the queue.

A queue is a linear data structure that follows FIFO (First In First Out) mechanism.

It consists of two variables: front and rear.

The front variable always points to the first node in the queue.

The rear variable always points to the last node in the queue.

Both front and rear refer to null when the queue is empty in a linked list.

Thus, the elements are inserted from the rear end and the elements are deleted from the front end.

import java.io.*;
class Node{
    int data;
    Node next;
    public Node(){
        data = 0;
        next = null;
    }
    public Node(int d){
        data = d;
        next = null;
    }
}
class LinkedQueue{
    Node front;
    Node rear;
    public LinkedQueue(){
        front = null;
        rear = null;
    }
    public void push(int d){
        Node n = new Node(d);
        if(rear == null){
            front = n;
            rear = n;
        }
        else{
            rear.next = n;
            rear = rear.next;
        }
        System.out.println(d + " pushed.");
    }
    public void pop(){
        if(front == null)
            System.out.println("Queue underflow!");
        else{
            int d = front.data;
            front = front.next;
            System.out.println(d + " popped.");
        }
        if(front == null)
            rear = null;
    }
    public void display(){
        if(front == null)
            System.out.println("Queue empty!");
        else{
            System.out.println("Queue contents:");
            Node n = front;
            while(n != null){
                System.out.print(n.data + "\t");
                n = n.next;
            }
            System.out.println();
        }
    }
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        LinkedQueue queue = new LinkedQueue();
        while(true){
            System.out.println("1. Push");
            System.out.println("2. Pop");
            System.out.println("3. Display");
            System.out.print("Enter your choice: ");
            int choice = Integer.parseInt(br.readLine());
            switch(choice){
                case 1:
                System.out.print("Element to be pushed: ");
                int d = Integer.parseInt(br.readLine());
                queue.push(d);
                break;
                case 2:
                queue.pop();
                break;
                case 3:
                queue.display();
                break;
                default:
                System.out.println("Bye...");
                return;
            }
        }
    }
}

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.