6 changed files with 0 additions and 258 deletions
@ -1,6 +0,0 @@ |
|||
package solution; |
|||
|
|||
public interface ICommand<E> { |
|||
|
|||
void execute(E e); |
|||
} |
@ -1,54 +0,0 @@ |
|||
package solution; |
|||
|
|||
import java.util.Collection; |
|||
import java.util.Comparator; |
|||
|
|||
public interface ISimpleList<E> { |
|||
|
|||
// Anzahl der Elemente in der Liste
|
|||
int size(); |
|||
|
|||
// fügt das Element e am Ende der Liste ein, sofern es ungleich null ist:
|
|||
// Liefert true, falls ein Element eingefügt wurde (false sonst)
|
|||
boolean add(E e); |
|||
|
|||
// fügt alle Elemente der Collection c ein (sofern diese nicht null sind).
|
|||
boolean add(Collection<E> c); |
|||
|
|||
// prüft, ob das Objekt o in der Liste enthalten ist (o == null führt zur
|
|||
// Rückgabe false)
|
|||
boolean contains(Object o); |
|||
|
|||
// entfernt das Objekt o aus der Liste // true: Ein Element wurde entfernt,
|
|||
// false sonst
|
|||
boolean remove(Object o); |
|||
|
|||
// Durchläuft alle Elemente der Liste und wendet command.execute auf jedes
|
|||
// Element an
|
|||
void forAll(ICommand<E> command); |
|||
|
|||
// fügt das Element an der Position i ein, sofern es nicht null ist und 0 <= i
|
|||
// <= Anzahl an Elementen gilt. Rückgabe true, false sonst.
|
|||
default void add(int i, E e) throws IndexOutOfBoundsException { |
|||
} |
|||
|
|||
// Liefert das Element, das an Position i steht, null falls i keinen gültigen
|
|||
// Index darstellt.
|
|||
default E get(int i) throws IndexOutOfBoundsException { |
|||
return null; |
|||
} |
|||
|
|||
// Ersetzt das i-te Element durch e, sofern es vorhanden ist (Index gültig)
|
|||
default void set(int i, E e) throws IndexOutOfBoundsException { |
|||
} |
|||
|
|||
// Löscht das i-te Element, sofern der Index gültig ist. Rückgabe true, false
|
|||
// sonst.
|
|||
default boolean remove(int i) { |
|||
return false; |
|||
} |
|||
|
|||
// Sortiermethode mittels Comparator
|
|||
default void sort(Comparator<E> comparator) { |
|||
} |
|||
} |
@ -1,36 +0,0 @@ |
|||
package solution; |
|||
|
|||
public class Node <E>{ |
|||
private Node<E> predecessor; // Vorgänger
|
|||
private Node<E> successor; // Nachfolger
|
|||
private E payload; // Nutzlast
|
|||
|
|||
//Konstruktor mit Parameter payload
|
|||
public Node(E payload) { |
|||
this.payload = payload; |
|||
} |
|||
|
|||
public Node<E> getPredecessor() { |
|||
return predecessor; |
|||
} |
|||
|
|||
public void setPredecessor(Node<E> predecessor) { |
|||
this.predecessor = predecessor; |
|||
} |
|||
|
|||
public Node<E> getSuccessor() { |
|||
return successor; |
|||
} |
|||
|
|||
public void setSuccessor(Node<E> e) { |
|||
this.successor = e; |
|||
} |
|||
|
|||
public E getPayload() { |
|||
return payload; |
|||
} |
|||
|
|||
public void setPayload(E payload) { |
|||
this.payload = payload; |
|||
} |
|||
} |
@ -1,152 +0,0 @@ |
|||
package solution; |
|||
|
|||
public class SimpleLinkedList<E> extends AbstractSimpleList<E> { |
|||
|
|||
Node<E> firstNode; |
|||
int size; |
|||
|
|||
public SimpleLinkedList() { |
|||
firstNode = null; |
|||
size = 0; |
|||
} |
|||
|
|||
@Override |
|||
public int size() { |
|||
return size; |
|||
} |
|||
|
|||
@Override |
|||
public boolean add(E e) { |
|||
if (e == null) { |
|||
return false; |
|||
} else if (firstNode == null) { |
|||
firstNode = new Node<E>(e); |
|||
} else { |
|||
Node<E> currentNode = firstNode; |
|||
while (currentNode != null) { |
|||
if (currentNode.getSuccessor() == null) { |
|||
break; |
|||
} |
|||
currentNode = currentNode.getSuccessor(); |
|||
} |
|||
Node<E> newNode = new Node<E>(e); |
|||
currentNode.setSuccessor(newNode); |
|||
newNode.setPredecessor(currentNode); |
|||
} |
|||
|
|||
size++; |
|||
return true; |
|||
} |
|||
|
|||
@Override |
|||
public boolean remove(Object o) { |
|||
if (o == null) { |
|||
return false; |
|||
} |
|||
Node<E> currentNode = firstNode; |
|||
while (currentNode != null) { |
|||
if (o.equals(currentNode.getPayload())) { |
|||
currentNode.setPayload(null); |
|||
size--; |
|||
return true; |
|||
} |
|||
|
|||
if (currentNode.getSuccessor() == null) { |
|||
break; |
|||
} |
|||
currentNode = currentNode.getSuccessor(); |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public boolean contains(Object o) { |
|||
if (o == null) { |
|||
return false; |
|||
} |
|||
Node<E> currentNode = firstNode; |
|||
while (currentNode != null) { |
|||
if (o.equals(currentNode.getPayload())) { |
|||
return true; |
|||
} |
|||
|
|||
currentNode = currentNode.getSuccessor(); |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public void forAll(ICommand<E> command) { |
|||
if (command != null) { |
|||
Node<E> currentNode = firstNode; |
|||
while (currentNode != null) { |
|||
command.execute(currentNode.getPayload()); |
|||
currentNode = currentNode.getSuccessor(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private Node<E> getIndex(int i) { |
|||
|
|||
if (i < 0 || size <= i) { |
|||
return null; |
|||
} |
|||
Node<E> currentNode = firstNode; |
|||
int currentIndex = 0; |
|||
while (currentNode != null) { |
|||
if (currentIndex >= i) { |
|||
|
|||
if (currentNode.getPayload() != null) { |
|||
return currentNode; |
|||
} |
|||
} |
|||
currentNode = currentNode.getSuccessor(); |
|||
currentIndex++; |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
public E get(int i) throws IndexOutOfBoundsException { |
|||
|
|||
return getIndex(i).getPayload(); |
|||
} |
|||
|
|||
public void set(int i, E e) throws IndexOutOfBoundsException { |
|||
|
|||
getIndex(i).setPayload(e); |
|||
} |
|||
|
|||
public boolean remove(int i) throws IndexOutOfBoundsException { |
|||
|
|||
if (getIndex(i) != null) { |
|||
getIndex(i).setPayload(null); |
|||
size--; |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
public void add(int i, E e) throws IndexOutOfBoundsException { |
|||
if (size == 0) { |
|||
if (i != 0) { |
|||
throw new IndexOutOfBoundsException("Index " + i + " is out of bounds!"); |
|||
} |
|||
set(0, e); |
|||
|
|||
} else if (i == size) { |
|||
add(e); |
|||
|
|||
} else if (size > 0 && i < size) { |
|||
Node<E> newNode = new Node<>(e);// node3
|
|||
|
|||
Node<E> tempNode = getIndex(i); // node2
|
|||
Node<E> preTempNode = getIndex(i).getPredecessor(); // node1
|
|||
|
|||
preTempNode.setSuccessor(newNode); |
|||
newNode.setPredecessor(preTempNode); |
|||
newNode.setSuccessor(tempNode); |
|||
tempNode.setPredecessor(newNode); |
|||
} |
|||
} |
|||
} |
@ -1,9 +0,0 @@ |
|||
package solution; |
|||
|
|||
public class SimpleLinkedListTest extends SimpleListTest90{ |
|||
|
|||
@Override |
|||
protected ISimpleList<String> getInstance1() { |
|||
return new SimpleLinkedList<>(); |
|||
} |
|||
} |
Loading…
Reference in new issue