In: Computer Science
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class.
public class Queue <T>{
}
I have put a driver program in the module . It is called CircularQueue.java
This driver program should then run with your Queue class (no modifications allowed to the driver program).
Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue, peek, isEmpty, getSize, makeEmpty, and printQ.
printQ takes one parameter (an integer) and has return type void. printQ(30) will print 30 elements in the queue. If the queue has fewer than 30 elements (and because it is circular) then you will just keep on going round and round until you have printed out all 30.
Upload your Queue class ONLY to canvas.
Queue Class
import java.util.LinkedList;
public class Queue<T> { //generic class
LinkedList<T> list; // defing linked list: - it implements
both list and dequeue interface
// Queue follows first in last out approach
Queue() { //constructor
list = new LinkedList<>(); //inititalizing linkedList
}
void enqueue(T element) { //add element at the last of the
list
list.addLast(element);
}
T dequeue() {
if (list.isEmpty() == false) {
return list.pollFirst(); //Retrieves and removes the first element
of this list
} else {
System.out.println("List is empty!");
return null;
}
}
int getSize() { // return size of the list
return list.size();
}
void peek() { //Retrieves, but does not remove, the first
element of this list.
if (list.isEmpty() == false) {
System.out.println(list.peek());
} else {
System.out.println("List is empty!");
}
}
boolean isEmpty() {
if (list.isEmpty()) {
return true;
} else {
return false;
}
}
void makeEmpty() {
list.removeAll(list);
}
Code must have
constructor
enqueue
dequeue
peek
isEmpty
getSize
makeEmpty
printQ
// Queue.java: Java program to implement a Circular Queue
import java.util.LinkedList;
public class Queue<T> {//generic class
LinkedList<T> list; // defining linked list:
- it implements both list and dequeue interface
// Queue follows first in last out approach
Queue() { //constructor
list = new LinkedList<>();
//inititalizing linkedList
}
void enqueue(T element) { //add element at the last of
the list
list.addLast(element);
}
T dequeue() {
if (list.isEmpty() == false)
{
return
list.pollFirst(); //Retrieves and removes the first element of this
list
} else {
System.out.println("List is empty!");
return
null;
}
}
int getSize() { // return size of the list
return list.size();
}
void peek() { //Retrieves, but does not remove, the
first element of this list.
if (list.isEmpty() == false)
{
System.out.println(list.peek());
} else {
System.out.println("List is empty!");
}
}
boolean isEmpty() {
if (list.isEmpty()) {
return
true;
} else {
return
false;
}
}
void makeEmpty() {
list.removeAll(list);
}
/** method that displays the elements of queue, the
number of elements displayed is specified by the argument.
* If the queue has fewer than num_elements then it
will just keep on going round and round until num_elements have
been displayed
* @param num_elements, integer specifying the number
of elements to display
*/
void printQ(int num_elements)
{
if(isEmpty()) // empty queue
System.out.println("List is empty!");
else
{
int index = 0;
// set index to index of first element i.e 0
// loop
num_elements times
for(int
i=0;i<num_elements;i++)
{
System.out.print(list.get(index)); // display
the element at index
index++; // increment index by 1
if(index == getSize()) // end of queue has been
reached, reset index to first element i.e 0
index = 0;
if(i < num_elements-1) // this is not the
last element, display a comma followed by a space
System.out.print(", ");
}
System.out.println(); // display a new line at the end
}
}
}
// end of Queue.java