diff --git a/SimpleLinkedList/.classpath b/SimpleLinkedList/.classpath new file mode 100644 index 0000000..e770324 --- /dev/null +++ b/SimpleLinkedList/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/SimpleLinkedList/.project b/SimpleLinkedList/.project new file mode 100644 index 0000000..5443afc --- /dev/null +++ b/SimpleLinkedList/.project @@ -0,0 +1,17 @@ + + + SimpleLinkedList + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/SimpleLinkedList/src/solution/Node.java b/SimpleLinkedList/src/solution/Node.java new file mode 100644 index 0000000..7e412f3 --- /dev/null +++ b/SimpleLinkedList/src/solution/Node.java @@ -0,0 +1,45 @@ +package solution; + +public class Node { + private Node predecessor; // Vorgänger + private Node successor; // Nachfolger + private E payload; // Nutzlast + + + public Node(E payload) { + this.payload = payload; + } + + + public Node getPredecessor() { + return predecessor; + } + + + public void setPredecessor(Node predecessor) { + this.predecessor = predecessor; + } + + + public Node getSuccessor() { + return successor; + } + + + public void setSuccessor(Node e) { + this.successor = e; + } + + + public E getPayload() { + return payload; + } + + + public void setPayload(E payload) { + this.payload = payload; + } + + + +} diff --git a/SimpleLinkedList/src/solution/SimpleLinkedList.java b/SimpleLinkedList/src/solution/SimpleLinkedList.java new file mode 100644 index 0000000..251d22e --- /dev/null +++ b/SimpleLinkedList/src/solution/SimpleLinkedList.java @@ -0,0 +1,92 @@ +package solution; + +//2017_OOP_90.jar importieren die von Bayer in der Aufgabenstellung zur Verfügung gestellt wird. + +import testat.*; + +public class SimpleLinkedList extends AbstractSimpleList { + + Node ersteKnoten; + int size; + + public SimpleLinkedList() { + ersteKnoten = null; + size = 0; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean add(E e) { + if (e == null) { + return false; + } else { + Node neuerKnoten = new Node<>(e); + + if (ersteKnoten == null) { + ersteKnoten = neuerKnoten; + } + else { + Node aktuellerKnoten = ersteKnoten; + while(aktuellerKnoten != null) { + aktuellerKnoten = aktuellerKnoten.getSuccessor(); + } + aktuellerKnoten.setSuccessor(neuerKnoten); + aktuellerKnoten.setPredecessor(aktuellerKnoten); + + } + } + size++; + return true; + } + + @Override + public boolean remove(Object o) { + if(o == null) { + return false; + } + Node aktuellerKnoten = ersteKnoten; + while(aktuellerKnoten != null) { + aktuellerKnoten = aktuellerKnoten.getSuccessor(); + } + if(o.equals(aktuellerKnoten)) { + aktuellerKnoten.setPayload(null); + size--; + aktuellerKnoten = aktuellerKnoten.getPredecessor(); + return true; + + } + return false; + } + + @Override + public boolean contains(Object o) { + if(o == null) { + return false; + } + Node aktuellerKnoten = ersteKnoten; + while(aktuellerKnoten != null) { + aktuellerKnoten = aktuellerKnoten.getSuccessor(); + } + if(o.equals(aktuellerKnoten)) { + return true; + } + return false; + + } + + @Override + public void forAll(ICommand command) { + if(command != null) { + Node aktuellerKnoten = ersteKnoten; + while(aktuellerKnoten != null) { + command.execute(aktuellerKnoten.getPayload()); + aktuellerKnoten = aktuellerKnoten.getSuccessor(); + } + } + } + +} diff --git a/SimpleLinkedList/src/solution/SimpleLinkedListTest.java b/SimpleLinkedList/src/solution/SimpleLinkedListTest.java new file mode 100644 index 0000000..b148ba4 --- /dev/null +++ b/SimpleLinkedList/src/solution/SimpleLinkedListTest.java @@ -0,0 +1,16 @@ +package solution; +import testat.*; + +public class SimpleLinkedListTest extends SimpleListTest90{ + + @Override + protected ISimpleList getInstance1() { + return new SimpleLinkedList<>(); + } + + @Override + protected ISimpleList> getInstance2() { + return new SimpleLinkedList<>(); + } + +}