Answered step by step
Verified Expert Solution
Link Copied!

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();     }     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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

Marketing strategy and the marketing mix

Answered: 1 week ago