Browse Source

final edits

master
David 4 years ago
parent
commit
7b9a116391
1 changed files with 15 additions and 16 deletions
  1. +15
    -16
      src/ChainQueue.java

+ 15
- 16
src/ChainQueue.java View File

@@ -33,15 +33,15 @@ public class ChainQueue<E> implements Queue<E> {


} }


Node<E> node; // first item
Node<E> first; // first item of the queue


@Override @Override
public boolean add(E input) { // Übergeben der "Data" public boolean add(E input) { // Übergeben der "Data"
if (node == null) { // Ist das Erste der Schlange leer?
node = new Node(input); // Ja? Direkt einfügen
if (first == null) { // is the first empty?
first = new Node(input); // yes? ->
return true; return true;
} }
return addNode(input, node); // Andernfalls die Schlage durchlaufen und dort einfügen
return addNode(input, first); // Andernfalls die Schlage durchlaufen und dort einfügen


} }


@@ -55,8 +55,8 @@ public class ChainQueue<E> implements Queue<E> {
} }


@Override @Override
public int size() {
Node temp = node;
public int size() { // run through the queue and count the size
Node temp = first;
int size = 0; int size = 0;
while (temp != null) { while (temp != null) {
size++; size++;
@@ -67,7 +67,7 @@ public class ChainQueue<E> implements Queue<E> {




@Override @Override
public boolean isEmpty() {
public boolean isEmpty() { // is the queue empty? -> size == 0
if (this.size() == 0) { if (this.size() == 0) {
return true; return true;
} }
@@ -75,11 +75,10 @@ public class ChainQueue<E> implements Queue<E> {
} }


@Override @Override
public boolean contains(Object o) {
Node temp = node;

public boolean contains(Object o) { // does object o exist in our queue?
Node temp = first;
for (int i = 0; i < this.size(); i++) { for (int i = 0; i < this.size(); i++) {
if (node.getData().equals(o)) {
if (first.getData().equals(o)) {
return true; return true;
} }
temp = temp.getNext(); temp = temp.getNext();
@@ -143,11 +142,11 @@ public class ChainQueue<E> implements Queue<E> {
} }


@Override @Override
public E poll() {
public E poll() { // poll next from queue and remove the first
E temp = null; E temp = null;
if (!this.isEmpty()) { if (!this.isEmpty()) {
temp = node.getData();
node = node.getNext();
temp = first.getData();
first = first.getNext();
} }
return temp; return temp;
} }
@@ -158,9 +157,9 @@ public class ChainQueue<E> implements Queue<E> {
} }


@Override @Override
public E peek() {
public E peek() { // only "see" into the queue and do not remove anything
if (!this.isEmpty()) { if (!this.isEmpty()) {
return node.getData();
return first.getData();
} }
return null; return null;
} }


Loading…
Cancel
Save