Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. need a CircArrayQueue representation for the Queue abstraction = [X, Y, Z]:5 in which Z has an array index greater than X, but first
1. need a CircArrayQueue representation for the Queue abstraction = [X, Y, Z]:5 in which Z has an array index greater than X, but first is not 0.
contents = ...
first = ...
length = ...
capacity = ...
2. need a CircArrayQueue representation for the Queue abstraction = [X, Y, Z]:5 in which Z has an array index less than X.
contents = ...
first = ...
length = ...
capacity = ...
3. create the Queue abstraction corresponding to the CircArrayQueue representation: contents = [W, X, Y, Z] and first =3 and length = 3.
queue = ...
public class CircArrayQueue<E> extends AbstractQueue<E> { private E[] elements; private int first; private int length; /** * @param max the bound of the new queue */ @SuppressWarnings("unchecked") public CircArrayQueue(int max) { super(max); elements = ((E[]) new Object[max]); length = max; first = 0; } @SuppressWarnings("unchecked") @Override public void enqueue(E element) throws IllegalStateException, IllegalArgumentException { if (element == null) { throw new IllegalArgumentException(); } if (first == length) { throw new IllegalStateException(); } Setset = new HashSet<>(Arrays.asList(elements)); set.remove(null); set.add(element); elements = (E[]) set.toArray(new String[] {}); first++; } @SuppressWarnings("unchecked") @Override public E dequeue() throws IllegalStateException { if (first == 0) { throw new IllegalStateException(); } E result = elements[elements.length - 1]; Set set = new HashSet<>(Arrays.asList(elements)); set.remove(null); set.remove(result); elements = (E[]) set.toArray(new String[] {}); first -= 1; return result; } @Override public int length() { return first; } @Override public Queue newInstance() { return new CircArrayQueue<>(capacity()); } @Override public void clear() { first = 0; } @Override public Iteratoriterator() { return new QueueIterator(); } private class QueueIterator implements Iterator<E> { private int currentIndex = 0; public boolean hasNext() { return currentIndex < first; } public E next() { if (!hasNext()) { throw new NoSuchElementException(); } E result = elements[currentIndex]; currentIndex++; return result; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started