Question: 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();     }     Set set = 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 Iterator iterator() {     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

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 Programming Questions!