import java.util.Collection; import java.util.Iterator; import java.util.Queue; public class ChainQueue implements Queue { private class Node { private E data; private Node next; public Node getNext() { return next; } public E getData() { return data; } public void setData(E data) { this.data = data; } public void setNext(Node next) { this.next = next; } public Node(E data) { this.data = data; } } Node first; // first item of the queue @Override public boolean add(E input) { // Übergeben der "Data" if (first == null) { // is the first empty? first = new Node(input); // yes? -> insert the node here return true; } return addNode(input, first); // no? -> goto addNode() } private boolean addNode(E input, Node n) { // run through the queue and find the first "place" which is empty if (n.getNext() == null) { n.setNext(new Node(input)); return true; } return addNode(input, n.getNext()); } @Override public int size() { // run through the queue and count the size Node temp = first; int size = 0; while (temp != null) { size++; temp = temp.getNext(); } return size; } @Override public boolean isEmpty() { // is the queue empty? -> size == 0 if (this.size() == 0) { return true; } return false; } @Override public boolean contains(Object o) { // does object o exist in our queue? Node temp = first; for (int i = 0; i < this.size(); i++) { if (first.getData().equals(o)) { return true; } temp = temp.getNext(); } return false; } @Override public Iterator iterator() { return null; } @Override public Object[] toArray() { return new Object[0]; } @Override public T[] toArray(T[] ts) { return null; } @Override public boolean remove(Object o) { return false; } @Override public boolean containsAll(Collection collection) { return false; } @Override public boolean addAll(Collection collection) { return false; } @Override public boolean removeAll(Collection collection) { return false; } @Override public boolean retainAll(Collection collection) { return false; } @Override public void clear() { } @Override public boolean offer(E e) { return false; } @Override public E remove() { return null; } @Override public E poll() { // poll next from queue and remove the first E temp = null; if (!this.isEmpty()) { temp = first.getData(); first = first.getNext(); } return temp; } @Override public E element() { return null; } @Override public E peek() { // only "see" into the queue and do not remove anything if (!this.isEmpty()) { return first.getData(); } return null; } }