ArrayQueue.java public class ArrayQueue implements QueueInterface { // Do not ad
ID: 3880557 • Letter: A
Question
ArrayQueue.java
public class ArrayQueue implements QueueInterface {
// Do not add new instance variables.
private T[] backingArray;
private int front;
private int size;
/**
* Constructs a new ArrayQueue.
*/
public ArrayQueue() {
}
/**
* Dequeue from the front of the queue.
*
* Do not shrink the backing array.
* If the queue becomes empty as a result of this call, you should
* explicitly reset front to 0.
*
* You should replace any spots that you dequeue from with null. Failure to
* do so can result in a loss of points.
*
* See the homework pdf for more information on implementation details.
*
* @see QueueInterface#dequeue()
*/
@Override
public T dequeue() {
}
/**
* Add the given data to the queue.
*
* If sufficient space is not available in the backing array, you should
* regrow it to double the current length. If a regrow is necessary,
* you should copy elements to the front of the new array and reset
* front to 0.
*
* @see QueueInterface#enqueue(T)
*/
@Override
public void enqueue(T data) {
}
@Override
public T peek() {
}
@Override
public boolean isEmpty() {
// DO NOT MODIFY THIS METHOD!
return size == 0;
}
@Override
public int size() {
// DO NOT MODIFY THIS METHOD!
return size;
}
/**
* Returns the backing array of this queue.
* Normally, you would not do this, but we need it for grading your work.
*
* DO NOT USE THIS METHOD IN YOUR CODE.
*
* @return the backing array
*/
public Object[] getBackingArray() {
// DO NOT MODIFY THIS METHOD!
return backingArray;
}
}
.
.
.
.
.
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
import java.util.NoSuchElementException;
public class ArrayQueue<T> implements QueueInterface<T> {
// Do not add new instance variables.
private T[] backingArray;
private int front;
private int size;
/**
* Constructs a new ArrayQueue.
*/
public ArrayQueue() {
backingArray = (T[])new Object[INITIAL_CAPACITY];
front = 0;
size = 0;
}
/**
* Dequeue from the front of the queue.
*
* Do not shrink the backing array.
* If the queue becomes empty as a result of this call, you should
* explicitly reset front to 0.
*
* You should replace any spots that you dequeue from with null. Failure to
* do so can result in a loss of points.
*
* See the homework pdf for more information on implementation details.
*
* @see QueueInterface#dequeue()
*/
/**
* Dequeue from the front of the queue.
*
* This method should be implemented in O(1) time.
*
* @return the data from the front of the queue
* @throws java.util.NoSuchElementException if the queue is empty
*/
@Override
public T dequeue() {
if(isEmpty())
throw new NoSuchElementException();
T val = backingArray[front];
backingArray[front] = null;
front++;
size--;
if(isEmpty())
front = 0;
return val;
}
/**
* Add the given data to the queue.
*
* If sufficient space is not available in the backing array, you should
* regrow it to double the current length. If a regrow is necessary,
* you should copy elements to the front of the new array and reset
* front to 0.
*
* @see QueueInterface#enqueue(T)
*/
/**
* Add the given data to the queue.
*
* This method should be implemented in (if array-backed, amortized) O(1)
* time.
*
* @param data the data to add
* @throws IllegalArgumentException if data is null
*/
@Override
public void enqueue(T data) {
int last = front + size;
//expand the array if the new data can't fit
if(last >= backingArray.length)
{
T[] temp = (T[]) new Object[2*backingArray.length];
for(int i = 0, j = front; i < size; i++, j++)
temp[i] = backingArray[j];
backingArray = temp;
front = 0;
last = size;
}
backingArray[last] = data;
}
@Override
public T peek() {
return backingArray[front];
}
@Override
public boolean isEmpty() {
// DO NOT MODIFY THIS METHOD!
return size == 0;
}
@Override
public int size() {
// DO NOT MODIFY THIS METHOD!
return size;
}
/**
* Returns the backing array of this queue.
* Normally, you would not do this, but we need it for grading your work.
*
* DO NOT USE THIS METHOD IN YOUR CODE.
*
* @return the backing array
*/
public Object[] getBackingArray() {
// DO NOT MODIFY THIS METHOD!
return backingArray;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.