Informatikks1/Queue.java

66 lines
1.2 KiB
Java

public class Queue<T>
{
private Node<T> first;
public Queue() {
first = null;
}
public boolean isEmpty() {
if (first == null) return true;
return false;
}
public T front(int n) {
Node<T> current = first;
for (int i = 0; i < n; i++) {
if (current == null) return null;
current = current.next;
}
if (current == null) return null;
return current.wert;
}
public void enqueue(T val) {
Node<T> neu = new Node<T>();
neu.wert = val;
if (first == null) {
first = neu;
} else {
Node<T> current = first;
while (current.next != null) {
current = current.next;
}
current.next = neu;
}
}
public T dequeue() {
T tmp = first.wert;
first = first.next;
return tmp;
}
public String toString(){
String result = "";
Node<T> current = first;
while (current != null) {
result += current.wert + ", ";
current = current.next;
}
return result;
}
}