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
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 _____________.   ...
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)
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...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue will call on a linkedlist class class Queue: def __init__(self): self.items = LinkedList() #has to be O(1) def enqueue(self, item): #has to be O(1) def dequeue(self): def is_empty(self): def __len__(self):
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;...
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.
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT