forked from IF-LK-2020/queue-stack
56 lines
1.2 KiB
Java
56 lines
1.2 KiB
Java
|
|
public class Queue<ContentType> {
|
|
|
|
private QueueNode<ContentType> head = null;
|
|
|
|
private QueueNode<ContentType> tail = null;
|
|
|
|
public Queue() {
|
|
}
|
|
|
|
public void enqueue( ContentType pContentObject ) {
|
|
if (head == null) {
|
|
head = new QueueNode<ContentType>(pContentObject);
|
|
tail = head;
|
|
} else {
|
|
QueueNode<ContentType> node = new QueueNode<ContentType>(pContentObject);
|
|
tail.setNext(node);
|
|
tail = node;
|
|
}
|
|
}
|
|
|
|
public void dequeue() {
|
|
if (head != tail) {
|
|
head = head.getNext();
|
|
} else if (head != null) {
|
|
head = null;
|
|
tail = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Liefert das Inhaltsobjekt vom ersten Knotens der Schlange
|
|
* vom Typ ContentType, falls die Schlange nicht leer ist,
|
|
* ansonsten null.
|
|
*
|
|
* @return Das Inhaltsobjekt oder null.
|
|
*/
|
|
public ContentType front() {
|
|
if( !isEmpty() ) {
|
|
return head.getContent();
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Prüft, ob die Schlange leer ist.
|
|
*
|
|
* @return true, wenn die Schlange keine Knoten enthaelt
|
|
*/
|
|
public boolean isEmpty() {
|
|
return (head == null);
|
|
}
|
|
|
|
}
|