Question: Write the following methods: (1) PrintQueue: Prints the queue contents. You are not allowed to use public methods. (2) PrintQueueReverse: Prints the queue contents in

Write the following methods:

(1) PrintQueue: Prints the queue contents. You are not allowed to use public methods.

(2) PrintQueueReverse: Prints the queue contents in reverse order. You are not allowed to use public methods or a stack.

public class ArrayQueue implements QueueInterface {

T[] queue; int frontIndex; int backIndex; private static final int DEFAULT_INITIAL_CAPACITY = 50;

public ArrayQueue() { this(DEFAULT_INITIAL_CAPACITY); }

public ArrayQueue(int initialCapacity) { // the cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] tempQueue = (T[]) new Object[initialCapacity + 1]; queue = tempQueue; frontIndex = 0; backIndex = initialCapacity; } // end constructor

@Override public void enqueue(T newEntry) {

ensureCapacity(); backIndex = (backIndex + 1) % queue.length; queue[backIndex] = newEntry;

}

private void ensureCapacity() { if (frontIndex == ((backIndex + 2) % queue.length)) // if array is full, { // double size of array T[] oldQueue = queue; int oldSize = oldQueue.length; // the cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] tempQueue = (T[]) new Object[2 * oldSize]; queue = tempQueue; for (int index = 0; index < oldSize - 1; index++) { queue[index] = oldQueue[frontIndex]; frontIndex = (frontIndex + 1) % oldSize; } // end for frontIndex = 0; backIndex = oldSize - 2; } // end if } // end ensureCapacity

@Override public T dequeue() { T front = null; if (!isEmpty()) { front = queue[frontIndex]; queue[frontIndex] = null; frontIndex = (frontIndex + 1) % queue.length; } // end if return front; }

@Override public T getFront() { T front = null; if (!isEmpty()) { front = queue[frontIndex]; } return front; }

@Override public boolean isEmpty() { return frontIndex == ((backIndex + 1) % queue.length); }

@Override public void clear() { frontIndex = 0; backIndex = DEFAULT_INITIAL_CAPACITY; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!