Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m a beginner in JAVA Create a generic queue class, implementing it using a li

ID: 3734826 • Letter: I

Question

I'm a beginner in JAVA

Create a generic queue class, implementing it using a linked list. Define a generic node class inside the queue class that will be made up of a data member of type T and a data member of the generic node type. The queue class will have three data members:

An object of the generic node class to store the reference to the first node in the queue

An object of the generic node class to store the reference to the last node in the queue

An integer to store the number of nodes in the queue

Code the following instance methods:

the zero-parameter constructor setting up an empty queue

public void addToQueue( T )       adds an element to the queue

public T deleteFromQueue( )   removes an element from the queue

public boolean isEmpty()      returns true if the queue is empty and false otherwise

public int size()              returns the number of elements in the queue

public T lookUp()              returns the first element in the queue without deleting it

public void clearQueue()      clears the queue ( reset it to be empty )

public String toString()      creates a string with the content of the queue

Write your own small main method (in a separate file) to test your queue class thoroughly.

Explanation / Answer

Please find my implementation:

public class GenericQueue<T> {

   // A linked list (LL) node to store a queue entry

   private class QNode<T>

   {

       T key;

       QNode<T> next;

       // constructor to create a new linked list node

       public QNode(T key) {

           this.key = key;

           this.next = null;

       }

   }

   private QNode<T> front, rear;

   private int N;

   public GenericQueue() {

       this.front = this.rear = null;

       N = 0;

   }

   // Method to add an key to the queue.

   public void addToQueue( T key)

   {

       // Create a new LL node

       QNode<T> temp = new QNode<>(key);

       // If queue is empty, then new node is front and rear both

       if (this.rear == null)

       {

           this.front = this.rear = temp;

           return;

       }

       // Add the new node at the end of queue and change rear

       this.rear.next = temp;

       this.rear = temp;

       N++;

   }

   // Method to remove an key from queue.

   public T deleteFromQueue( )

   {

       // If queue is empty, return NULL.

       if (this.front == null)

           return null;

       // Store previous front and move front one node ahead

       QNode<T> temp = this.front;

       this.front = this.front.next;

       // If front becomes NULL, then change rear also as NULL

       if (this.front == null)

           this.rear = null;

       N--;

       return temp.key;

   }

   public boolean isEmpty() {

       return N == 0;

   }

   public int size() {

       return N;

   }

   public T lookUp() {

       // If queue is empty, return NULL.

       if (this.front == null)

           return null;

       return front.key;

   }

   public void clearQueue() {

       this.front = this.rear = null;

       N = 0;

   }

   public String toString() {

       String res = "";

       QNode<T> t = front;

       while(t != null) {

           res = res + t.key+" ";

           t = t.next;

       }

       return res;

   }

}

############

public class GenericQueueTest {

  

   public static void main(String[] args) {

      

       GenericQueue<Integer> queue = new GenericQueue<>();

      

       queue.addToQueue(4);

       queue.addToQueue(5);

      

       System.out.println(queue);

      

       System.out.println("Dequeue:" +queue.deleteFromQueue());

      

       System.out.println(queue);

   }

}

/*

Sample run:

4 5

Dequeue:4

5

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote