|
|
@@ -35,15 +35,23 @@ public class ChainQueue<E> implements Queue<E> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Node<E> first; // first item of the queue |
|
|
|
private Node<E> first; // first item of the queue |
|
|
|
|
|
|
|
public Node<E> getFirst() { |
|
|
|
return first; |
|
|
|
} |
|
|
|
|
|
|
|
public void setFirst(Node<E> first) { |
|
|
|
this.first = first; |
|
|
|
} |
|
|
|
|
|
|
|
@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 |
|
|
|
if (getFirst() == null) { // is the first empty? |
|
|
|
setFirst(new Node(input)); // yes? -> insert the node here |
|
|
|
return true; |
|
|
|
} |
|
|
|
return addNode(input, first); // no? -> goto addNode() |
|
|
|
return addNode(input, getFirst()); // no? -> goto addNode() |
|
|
|
} |
|
|
|
|
|
|
|
private boolean addNode(E input, Node n) { // run through the queue and find the first "place" which is empty |
|
|
@@ -56,7 +64,7 @@ public class ChainQueue<E> implements Queue<E> { |
|
|
|
|
|
|
|
@Override |
|
|
|
public int size() { // run through the queue and count the size |
|
|
|
Node temp = first; |
|
|
|
Node temp = getFirst(); |
|
|
|
int size = 0; |
|
|
|
while (temp != null) { |
|
|
|
size++; |
|
|
@@ -76,9 +84,9 @@ public class ChainQueue<E> implements Queue<E> { |
|
|
|
|
|
|
|
@Override |
|
|
|
public boolean contains(Object o) { // does object o exist in our queue? |
|
|
|
Node temp = first; |
|
|
|
Node temp = getFirst(); |
|
|
|
for (int i = 0; i < this.size(); i++) { |
|
|
|
if (first.getData().equals(o)) { |
|
|
|
if (getFirst().getData().equals(o)) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
temp = temp.getNext(); |
|
|
@@ -145,8 +153,8 @@ public class ChainQueue<E> implements Queue<E> { |
|
|
|
public E poll() { // poll next from queue and remove the first |
|
|
|
E temp = null; |
|
|
|
if (!this.isEmpty()) { |
|
|
|
temp = first.getData(); |
|
|
|
first = first.getNext(); |
|
|
|
temp = getFirst().getData(); |
|
|
|
setFirst(getFirst().getNext()); |
|
|
|
} |
|
|
|
return temp; |
|
|
|
} |
|
|
@@ -159,9 +167,8 @@ public class ChainQueue<E> implements Queue<E> { |
|
|
|
@Override |
|
|
|
public E peek() { // only "see" into the queue and do not remove anything |
|
|
|
if (!this.isEmpty()) { |
|
|
|
return first.getData(); |
|
|
|
return getFirst().getData(); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
} |