CircularArrayQueue represents an array implementation of a queue in * which the indexes for the front and rear of the queue circle back to 0 * when they reach the end of the array. * * @author ITSC2214 * @version 4.0 */ public class CircularArrayQueue implements QueueADT { private final static int DEFAULT_CAPACITY = 100; private int front, rear, count; private T[] queue; /** * Creates an empty queue using the specified capacity. * @param initialCapacity the initial size of the circular array queue */ public CircularArrayQueue (int initialCapacity) { front = rear = count = 0; queue = (T[]) (new Object[initialCapacity]); } /** * Creates an empty queue using the default capacity. */ public CircularArrayQueue() { this(DEFAULT_CAPACITY); } /** * Adds the specified element to the rear of this queue, expanding * the capacity of the queue array if necessary. * @param element the element to add to the rear of the queue */ public void enqueue(T element) { if (size() == queue.length) expandCapacity(); queue[rear] = element; //TODO Update the rear variable correspondently count++; } /** * Creates a new array to store the contents of this queue with * twice the capacity of the old one. */ private void expandCapacity() { T[] larger = (T[]) (new Object[queue.length *2]); for (int scan = 0; scan < count; scan++) { larger[scan] = queue[front]; front = (front + 1) % queue.length; } front = 0; rear = count; queue = larger; } /** * Removes the element at the front of this queue and returns a * reference to it. * @return the element removed from the front of the queue * @throws EmptyCollectionException if the queue is empty */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("queue"); T result = queue[front]; queue[front] = null; //TODO Update the front variable correspondently count--; return result; } /** * Returns a reference to the element at the front of this queue. * The element is not removed from the queue. * @return the first element in the queue * @throws EmptyCollectionException if the queue is empty */ public T first() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("queue"); // To be completed as a Programming Project return queue[front]; } /** * Returns true if this queue is empty and false otherwise. * @return true if this queue is empty */ public boolean isEmpty() { if (count == 0) return true; else return false; } /** * Returns the number of elements currently in this queue. * @return the size of the queue */ public int size() { return count; } /** * Returns a string representation of this queue. * @return the string representation of the queue */ public String toString() { String txt = "Number of elements in queues:" + this.size() + "\n"; // Scan and print elements in the queue in the order of front through rear for (int i = 0; i < this.size(); i++){ // TODO Fill in an expression to retrieve the index of the i-th element in the order of front through rear txt += queue[(front + i) % queue.length].toString() + "\n"; } return txt;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

/**
* CircularArrayQueue represents an array implementation of a queue in
* which the indexes for the front and rear of the queue circle back to 0
* when they reach the end of the array.
*
* @author ITSC2214
* @version 4.0
*/
public class CircularArrayQueue<T> implements QueueADT<T>
{
private final static int DEFAULT_CAPACITY = 100;
private int front, rear, count;
private T[] queue;

/**
* Creates an empty queue using the specified capacity.
* @param initialCapacity the initial size of the circular array queue
*/
public CircularArrayQueue (int initialCapacity)
{
front = rear = count = 0;
queue = (T[]) (new Object[initialCapacity]);
}

/**
* Creates an empty queue using the default capacity.
*/
public CircularArrayQueue()
{
this(DEFAULT_CAPACITY);
}

/**
* Adds the specified element to the rear of this queue, expanding
* the capacity of the queue array if necessary.
* @param element the element to add to the rear of the queue
*/
public void enqueue(T element)
{
if (size() == queue.length)
expandCapacity();

queue[rear] = element;

//TODO Update the rear variable correspondently


count++;
}

/**
* Creates a new array to store the contents of this queue with
* twice the capacity of the old one.
*/
private void expandCapacity()
{
T[] larger = (T[]) (new Object[queue.length *2]);

for (int scan = 0; scan < count; scan++)
{
larger[scan] = queue[front];
front = (front + 1) % queue.length;
}

front = 0;
rear = count;
queue = larger;
}

/**
* Removes the element at the front of this queue and returns a
* reference to it.
* @return the element removed from the front of the queue
* @throws EmptyCollectionException if the queue is empty
*/
public T dequeue() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("queue");

T result = queue[front];
queue[front] = null;

//TODO Update the front variable correspondently


count--;

return result;
}

/**
* Returns a reference to the element at the front of this queue.
* The element is not removed from the queue.
* @return the first element in the queue
* @throws EmptyCollectionException if the queue is empty
*/
public T first() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("queue");

// To be completed as a Programming Project
return queue[front];
}

/**
* Returns true if this queue is empty and false otherwise.
* @return true if this queue is empty
*/
public boolean isEmpty()
{
if (count == 0)
return true;
else
return false;
}

/**
* Returns the number of elements currently in this queue.
* @return the size of the queue
*/
public int size()
{
return count;
}

/**
* Returns a string representation of this queue.
* @return the string representation of the queue
*/
public String toString()
{
String txt = "Number of elements in queues:" + this.size() + "\n";

// Scan and print elements in the queue in the order of front through rear
for (int i = 0; i < this.size(); i++){
// TODO Fill in an expression to retrieve the index of the i-th element in the order of front through rear
txt += queue[(front + i) % queue.length].toString() + "\n";
}

return txt;
}
}

 

Open and read CircularArrayQueue.java in the src/DataStructures sub-folder. Here we assume
that you have download the start code package, and launch it in NetBeans, as shown in the
preparation part of the lab description.
• Fill in missing statements in enqueuel) and dequeue) method
Transcribed Image Text:Open and read CircularArrayQueue.java in the src/DataStructures sub-folder. Here we assume that you have download the start code package, and launch it in NetBeans, as shown in the preparation part of the lab description. • Fill in missing statements in enqueuel) and dequeue) method
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education