I need help with the following Java code Implement ADT queue by using a circular
ID: 3902405 • Letter: I
Question
I need help with the following Java code
Implement ADT queue by using a circular linked chain (shown below). Recall that this chain has only an external reference to its last node. Name your class CircularLinkedQueue.
Here is the interface the class needs to use
/**
An interface for the ADT queue.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public interface QueueInterface<T>
{
/** Adds a new entry to the back of this queue.
@param newEntry An object to be added. */
public void enqueue(T newEntry);
/** Removes and returns the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty before the operation. */
public T dequeue();
/** Retrieves the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty. */
public T getFront();
/** Detects whether this queue is empty.
@return True if the queue is empty, or false otherwise. */
public boolean isEmpty();
/** Removes all entries from this queue. */
public void clear();
} // end QueueInterface
Explanation / Answer
import java.io.*;
import java.util.*;
class Node<T> {
public T data;
public Node<T> next;
}
interface QueueInterface<T>
{
/** Adds a new entry to the back of this queue.
@param newEntry An object to be added. */
public void enqueue(T newEntry);
/** Removes and returns the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty before the operation. */
public T dequeue();
/** Retrieves the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty. */
public T getFront();
/** Detects whether this queue is empty.
@return True if the queue is empty, or false otherwise. */
public boolean isEmpty();
/** Removes all entries from this queue. */
public void clear();
} // end QueueInterface
class CircularQueue<T> implements QueueInterface<T> {
private Node<T> front;
private Node<T> last;
public CircularQueue(){
front = null;
last = null;
}
public void clear(){
front = null;
last = null;
}
public T getFront(){
if (front != null)
return front.data;
return null;
}
public boolean isEmpty(){
if (front == null)
return true;
return false;
}
public void enqueue(T a){
Node<T> b = new Node<T>();
b.data = a;
b.next = null;
if (front == null){
front = b;
b.next = front;
last = b;
}
else {
b.next = front;
last.next = b;
last = b;
}
}
public T dequeue(){
if (front != null){
last.next = front.next;
T a = front.data;
front = front.next;
return a;
}
return null;
}
public void display(){
Node<T> p = front;
if (p != null){
while (p != last){
System.out.print(p.data + " ");
p = p.next;
}
System.out.print(p.data);
}
}
}
public class DemoCircularQueue{
public static void main(String[] args){
CircularQueue<Integer> c = new CircularQueue<Integer>();
QueueInterface<Integer> q = c;
q.enqueue(6);
q.enqueue(7);
q.enqueue(8);
System.out.println(q.getFront());
}
}