Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Iterator;
- // ARRAY QUEUE. A fixed length queue implemented as a circular array.
- class ArrayQueue<Base>
- {
- private class ArrayQueueIterator implements Iterator<Base>
- {
- // constructor
- private ArrayQueueIterator(...)
- {
- }
- // return true if ArrayQueue has more elements to visit
- // use ArrayQueueIterator's private vars only, use ideas from ArrayQueue's isEmpty.
- public boolean hasNext()
- {
- }
- // return next Base from ArrayQueue. If hasNext is false, throw IllegalStateException.
- // Use private vars only. Use ideas from dequeue.
- public Base next()
- {
- }
- public void remove()
- { }
- }
- private int front;
- private int rear;
- private Base [] bases;
- public ArrayQueue(int size)
- {
- if (size >= 1)
- {
- front = 0;
- rear = 0;
- bases = (Base []) new Object[size];
- }
- else
- {
- throw new IllegalArgumentException("Size must be at least 1.");
- }
- }
- public Base dequeue()
- {
- if (isEmpty())
- {
- throw new IllegalStateException("Queue is empty.");
- }
- else
- {
- front = (front + 1) % bases.length;
- Base temp = bases[front];
- bases[front] = null;
- return temp;
- }
- }
- public void enqueue(Base base)
- {
- int nextRear = (rear + 1) % bases.length;
- if (front == nextRear)
- {
- throw new IllegalStateException("Queue is full.");
- }
- else
- {
- rear = nextRear;
- bases[rear] = base;
- }
- }
- public boolean isEmpty()
- {
- return front == rear;
- }
- public boolean isFull()
- {
- return front == (rear + 1) % bases.length;
- }
- // Make a new instance of ArrayQueueIterator and return it.
- public Iterator<Base> iterator()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement