I need the JAVA code for the following program described bel
I need the JAVA code for the following program described below. I have provided the driver file mentioned in the description. Please follow all rules mentioned in assignment. Please do not submit a written picture of code but typed response. Thank you so much! Let me know if you have any questions about this!
Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver(below) to then test each of the methods. Simply download it and place it with the other source files.
Driver File:
public class QueuesTester {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(\"Testing Generic Linked List Queue\");
System.out.println(\"Enqueue\'ing 10 numbers 0-9\");
GenLLQueue<Integer> qLLInts = new GenLLQueue();
for(int i=0;i<10;i++)
{
qLLInts.enqueue(i);
}
System.out.println(\"Dequeue\'ing all numbers and printing them out.\");
for(int i=0;i<10;i++)
{
System.out.println(qLLInts.dequeue());
}
System.out.println(\"Testing peek\");
qLLInts.enqueue(5);
System.out.println(qLLInts.peek());
System.out.println(\"Testing show queue\");
for(int i=0;i<10;i+=2)
{
qLLInts.enqueue(i);
}
qLLInts.showQueue();
}
}
Create a class GenLLQueue which has the following:
Internal class ListNode which contains:
Instance variable data of type T
Instance variable link of type ListNode
Default constructor that sets both instance variables to null
Instance Variables
head which is of type ListNode which points to the first element in the queue
tail which of type ListNode which points to the last element in the queue
Constructor
A default constructor that initializes head and tail to null
Methods
enqueue – This method returns no value and takes a variable of type T and adds it after the tail. The moves to tail to point to the newly added element.
dequeue – This method removes and returns the first element in the queue
peek – This method returns the first element of the queue without removing it
showQueue – Prints out the queue in order
Example Dialog:
Testing Generic Linked List Queue
Enqueue\'ing 10 numbers 0-9
Dequeue\'ing all numbers and printing them out.
0
1
2
3
4
5
6
7
8
9
Testing peek
5
Testing show queue by adding all even numbers 0 to 8
5
0
2
4
6
8
Solution
CODE:
package temp;
// This exception is required for checking if the queue is Empty.
import java.util.NoSuchElementException;
// Generic class accepting any datatype.
class GenLLQueue<T>{
// Internal Node.
class ListNode<T>{
T data;
ListNode<T> link;
// Constructor
public ListNode(T data, ListNode<T> link) {
this.data = data;
this.link = link;
}
}
// Instance variables.
ListNode<T> head;
ListNode<T> tail;
// Constructor.
public GenLLQueue() {
super();
this.head = head;
this.tail = tail;
}
// Enqueue method
public void enqueue(T data){
// Create node with given data.
ListNode<T> tmp = new ListNode<T>(data,null);
// Check if there is only 1 node in the Queue.
// if yes, then both the head and tail points to the tmp.
if(tail == null){
head = tmp;
tail = tmp;
}
// If not, adjust the pointers to add the node to last node
// and adjust the pointers.
else{
tail.link = tmp;
tail = tail.link;
}
}
// Dequeue method
public T dequeue(){
// If the Queue is empty, raise an exception.
if(head == null)
throw new NoSuchElementException(\"Underflow Exception\");
// Remove the first node and return the value.
ListNode<T> tmp = head;
head = tmp.link;
if(head == null)
tail = null;
return tmp.data;
}
// peek() to just return the top element.
public T peek(){
// If the Queue is empty, raise an exception.
if(head == null)
throw new NoSuchElementException(\"Underflow Exception\");
// return the first element.
return head.data;
}
// display the elements of the queue.
public void showQueue(){
System.out.print(\"\ Queue = \");
// If the queue is empty, then print EMPTY.
if (head == null)
{
System.out.print(\"EMPTY\ \");
return ;
}
// Print out the elements of the queue.
ListNode<T> ptr = head;
while (ptr != tail )
{
System.out.print(ptr.data+\" \");
ptr = ptr.link;
}
System.out.println();
}
}
// Driver program given by yourself.
public class QueuesTester {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(\"Testing Generic Linked List Queue\");
System.out.println(\"Enqueue\'ing 10 numbers 0-9\");
GenLLQueue<Integer> qLLInts = new GenLLQueue();
for(int i=0;i<10;i++)
{
qLLInts.enqueue(i);
}
System.out.println(\"Dequeue\'ing all numbers and printing them out.\");
for(int i=0;i<10;i++)
{
System.out.println(qLLInts.dequeue());
}
System.out.println(\"Testing peek\");
qLLInts.enqueue(5);
System.out.println(qLLInts.peek());
System.out.println(\"Testing show queue\");
for(int i=0;i<10;i+=2)
{
qLLInts.enqueue(i);
}
qLLInts.showQueue();
}
}
OUTPUT:
Testing Generic Linked List Queue
Enqueue\'ing 10 numbers 0-9
Dequeue\'ing all numbers and printing them out.
0
1
2
3
4
5
6
7
8
9
Testing peek
5
Testing show queue
Queue = 5 0 2 4 6




