-
/**
-
* Jednoduchá fronta realizovaná
statickým polem.
-
* @author Peter
Williams
-
* @author Vojtěch
Hordějčuk
-
* @param <E> třída
obsaženého prvku
-
*/
-
public class
Queue<E>
-
{
-
/**
-
* pole s prvky
fronty
-
*/
-
private Object[] queue;
-
/**
-
* počet prvků ve
frontě
-
*/
-
private int size;
-
/**
-
* index, na který se bude
vkládat
-
*/
-
private int top;
-
/**
-
* index, ze kterého se bude
mazat
-
*/
-
private int base;
-
-
/**
-
* Vytvoří novou
frontu.
-
* @param capacity kapacita
fronty
-
*/
-
public Queue(int capacity)
-
{
-
queue = new Object[capacity];
-
size = 0;
-
top = 0;
-
base = 0;
-
}
-
-
/**
-
* Ověří, zda je fronta
prázdná.
-
* @return TRUE právě když
je fronta prázdná
-
*/
-
public boolean isEmpty()
-
{
-
return size == 0;
-
}
-
-
/**
-
* Ověří, zda je fronta
plná.
-
* @return TRUE právě když
je fronta plná
-
*/
-
public boolean isFull()
-
{
-
return size ==
queue.length;
-
}
-
-
/**
-
* Přidá prvek na konec
fronty.
-
* @param item vkládaný
prvek
-
*/
-
public void enqueue(E item)
-
{
-
if (isFull())
-
{
-
throw new RuntimeException("queue overflow");
-
}
-
queue[top] = item;
-
top = (top + 1) % queue.length;
-
size++;
-
}
-
-
/**
-
* Vrátí a vyjme prvek na
začátku fronty.
-
* @return první prvek ve
frontě
-
*/
-
@SuppressWarnings("unchecked")
-
public E dequeue()
-
{
-
if (isEmpty())
-
{
-
throw new RuntimeException("queue
underflow");
-
}
-
Object item =
queue[base];
-
base = (base + 1) % queue.length;
-
size--;
-
return (E) item;
-
}
-
}