Boa noite Galera!
Tenho a seguinte classe Genérica, uma estrutura de dados para Filas Estáticas.
Gostaria de saber qual a melhor forma de percorrer uma fila? Procurar um elemento na fila mantendo a ordem da fila.
public class StaticQueue implements Queue {
// Index to the first element
protected int first;
// Index to the last element
protected int last;
// Generic array used to store the elements
protected E elements[];
public StaticQueue(int maxSize) {
elements = (E[]) new Object[maxSize];
first = last = -1;
}
/** Testes whether the queue is empty. */
public boolean isEmpty() {
return first == -1;
}
public boolean isFull() {
return first == ((last + 1) % elements.length);
}
/** Returns the number of elements in the queue. */
public int numElements() {
if (isEmpty())
return 0;
else {
int n = elements.length;
return ((n + this.last - this.first) % n) + 1;
}
}
/** Inspects the element at the front of the queue. */
public E front() throws UnderflowException {
if (isEmpty())
throw new UnderflowException();
return elements[first];
}
/** Inspects the element at the back of the queue. */
public E back() throws UnderflowException {
if (isEmpty())
throw new UnderflowException();
return elements[last];
}
/** Inserts an element at the rear of the queue. */
public void enqueue(E element) throws OverflowException {
if (isFull())
throw new OverflowException("Lista cheia");
else {
if (last == -1)
first = last = 0;
else
last = (last + 1) % elements.length;
elements[last] = element;
}
}
/** Removes and return the element at the front of the queue. */
public E dequeue() throws UnderflowException {
if (isEmpty())
throw new UnderflowException();
E element = elements[first];
elements[first] = null; // p/ coleta de lixo
if (first == last)
first = last = -1;
else
first = (first + 1) % elements.length;
return element;
}
}