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

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
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 }...
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)
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
In the previous lab you created a Car class and a Dealership class. Now, in this...
In the previous lab you created a Car class and a Dealership class. Now, in this activity change the design of the Dealership class, in which the list of the cars inside a dealership will be stored in an ArrayList. Then, provide required getter and setter methods to keep the records of all its cars. In your tester class, test your class and also printout the list of all cars of a given dealership object. // Java program import java.util.ArrayList;...
Delete an item from a circular queue and return the index of the next item. (in...
Delete an item from a circular queue and return the index of the next item. (in Java)
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being...
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being served at the Department of Motor Vehicles. Start with 100 customers in a List. In a loop, generate a priority for each customer (1-5) In a second loop, add the users to a priority queue Print the List and the Priority Queue
Make up a holistic alter-ego "Ideal" alter-ego give him or her a name, and then describe...
Make up a holistic alter-ego "Ideal" alter-ego give him or her a name, and then describe his or her qualities,looks,strenghts, and how he or she goes about living a healthy, well-balanced life.
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked list. One way means that each node has only one pointer, Q, (not both a rear and a front pointer), so the program must use pointers-to-pointers. Include functions to insert(), remove(), for use in main() where the user is prompted "Enter "i" to insert a new element, "r" to remove an element, "q" to quit:"
In this exercise you will return to your SpecialityCoffee class which you created for the last...
In this exercise you will return to your SpecialityCoffee class which you created for the last coding activity (a sample solution to this exercise will be shown below the entire question if you do not have yours). This time you will override the Coffee method getPrice which returns the price in cents for a given coffee. The SpecialityCoffee method getPrice should return the price given by the Coffee method for that object, plus an extra charge for the flavored syrup....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT