Question

In: Computer Science

You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...

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

Solutions

Expert Solution

// 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


Related Solutions

Implementation of a a queue with a "circular" array or a "regular" array. (a) "circular" array:...
Implementation of a a queue with a "circular" array or a "regular" array. (a) "circular" array: consider using the smallest number of fields/variables in a struct/class. List the fields/variables that support the enqueue and dequeue operations in O(1) time in the worst case. Explain why the list is the smallest and the two operations are O(1) time. (b) "regular" array: explain the worst-case time complexity in big-O for the operations (queue size is at most n).
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node will have a 4 digit job number and a priority (A, B, etc) with A highest priority In the driver Create a priority queue object Add 3 jobs of 'B' priority Add 4 jobs of 'D' priority Add 2 jobs of highest priority Print the queue
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
C# Questions: 55.A class must be ___________________ before objects of that class can be created. a.added...
C# Questions: 55.A class must be ___________________ before objects of that class can be created. a.added to the Form b.declared as Private c.contain properties d.added to the application’s project 60.If the variable type of the element in a foreach statement cannot be converted to the same variable type as the collection’s objects, ______________. a.the body of the statement is ignored b.a syntax error occurs c.a runtime error occurs d.a logic error occurs 66.C# views each file as a _____________.   ...
Explain how do you do indexing in a circular queue? (java programing)
Explain how do you do indexing in a circular queue? (java programing)
Why a circular queue is more benefiting than a single dimension array queue? How to do...
Why a circular queue is more benefiting than a single dimension array queue? How to do indexing in a circular queue? (explain briefly in java)
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue object and try various operations below. Queue myQueue = new Queue(); myQueue.enqueue(“CPS 123”); myQueue.enqueue(“CPS 223”); myQueue.enqueue(“CPS 323”); myQueue.dequeue(); myQueue.enqueue(“CPS 113”); myQueue.enqueue(“CPS 153”); string course = myQueue.front(); // course should be CPS 223 size = myQueue.size(); // size should be 4 // output course and size
Why a circular queue is more benefiting than a single dimension array queue? How to do...
Why a circular queue is more benefiting than a single dimension array queue? How to do indexing in a circular queue? (In java)
You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables....
You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables. The first instance variable will store the days the bear has been awake. The second instance variable will store the number of teeth for the bear. The AngryBear class will have 1 constructor that takes in values for days awake and number of teeth. The AngryBear class will have one method isAngry(); An AngryBear is angry if it has been awake for more than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT