Browse Source

Crashkurs Tag 1 Gruppenlösung Testat List

weitere_testate
hertero 3 years ago
parent
commit
495693624b
  1. 11
      .project
  2. 5
      SimpleLinkedList/src/solution/ICommand.java
  3. 47
      SimpleLinkedList/src/solution/ISimpleList.java
  4. 31
      SimpleLinkedList/src/solution/Node.java
  5. 182
      SimpleLinkedList/src/solution/SimpleLinkedList11111.java
  6. 11
      SimpleLinkedList/src/solution/SimpleLinkedListTest11111.java

11
.project

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>oop_crashkurs</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>

5
SimpleLinkedList/src/solution/ICommand.java

@ -0,0 +1,5 @@
package solution;
public interface ICommand<E> {
void execute(E e);
}

47
SimpleLinkedList/src/solution/ISimpleList.java

@ -0,0 +1,47 @@
package solution;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
public interface ISimpleList<E> {
int size(); // Anzahl der Elemente in der Liste
boolean add(E e); // 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(Collection<E> c); // fügt alle Elemente der Collection c ein (sofern diese nicht null sind).
boolean contains(Object o); // prüft, ob das Objekt o in der Liste enthalten ist (o == null führt zur
// Rückgabe false)
boolean remove(Object o); // entfernt das Objekt o aus der Liste // true: Ein Element wurde entfernt,
// false sonst
void forAll(ICommand<E> command); // Durchläuft alle Elemente der Liste und wendet command.execute auf jedes
// Element an
default void add(int i, E e) throws IndexOutOfBoundsException {
} // 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 E get(int i) throws IndexOutOfBoundsException {
return null;
}
// Liefert das Element, das an Position i steht, null falls i keinen gültigen Index darstellt.
default void set(int i, E e) throws IndexOutOfBoundsException {
}
// Ersetzt das i-te Element durch e, sofern es vorhanden ist (Index gültig)
default boolean remove(int i) {
return false;
} // Löscht das i-te Element, sofern der Index gültig ist. Rückgabe true, false
// sonst.
default void sort(Comparator<E> comparator) {
}
}

31
SimpleLinkedList/src/solution/Node.java

@ -0,0 +1,31 @@
package solution;
public class Node<E> {
private Node<E> predecessor; // Vorgänger
private Node<E> successor; // Nachfolger
private E payload; // Nutzlast
public Node(E payload) {
super();
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> successor) {
this.successor = successor;
}
public E getPayload() {
return payload;
}
public void setPayload(E payload) {
this.payload = payload;
}
}

182
SimpleLinkedList/src/solution/SimpleLinkedList11111.java

@ -0,0 +1,182 @@
package solution;
public class SimpleLinkedList11111<E> extends AbstractSimpleList<E> {
private Node<E> firstNode;
private int size;
private Node<E> get(Object o) {
if (firstNode == null)
return null;
boolean loop = true;
Node<E> tmp = firstNode;
while (loop) { // hier kann auf die Nutzlast zugegriffen werden (payolad) if(Nachfolger von
if (o == null & tmp.getSuccessor() == null) {
return tmp;
}
if (o != null & tmp.getPayload().equals(o)) {
return tmp;
}
loop = tmp.getSuccessor() != null;
tmp = tmp.getSuccessor();
}
return null;
}
@Override
public int size() {
return size;
}
@Override
public boolean add(E e) {
if (e == null)
return false;
if (firstNode == null) {
firstNode = new Node<E>(e);
size++;
return true;
}
boolean loop = true;
Node<E> tmp = firstNode;
while (loop) {
loop = tmp.getSuccessor() != null;
if (!loop) {
Node<E> tmp2 = new Node<E>(e);
tmp.setSuccessor(tmp2);
tmp2.setPredecessor(tmp);
size++;
return true;
}
tmp = tmp.getSuccessor();
}
return false;
}
@Override
public boolean contains(Object o) {
// return this.get(o) != null;
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 boolean remove(Object o) {
// if(this.contains(o)) {
// this.get(o).setPayload(null);
// return true;
// };
// return false;
//
if (o == null)
return false;
Node<E> currentNode = firstNode;
while (currentNode != null) {
if (o.equals(currentNode.getPayload())) {
currentNode.setPayload(null);
size--;
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) {
Node<E> currentNode = firstNode;
int currIndex = 0;
while (currentNode != null) {
if (currIndex >= i) {
if (currentNode.getPayload() != null) {
return currentNode;
}
}
currentNode = currentNode.getSuccessor();
currIndex++;
}
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 void add(int i, E e) throws IndexOutOfBoundsException {
if (size == 0) {
if (i != 0) {
throw new IndexOutOfBoundsException("Index " + " out of bounds!");
}
set(0, e);
} else if (i == size) {
add(e);
} else if (size > 0 && i < size) {
Node<E> newNode = new Node<E>(e);
Node<E> tempNode = getIndex(i);
Node<E> preTempNode = getIndex(i).getPredecessor();
preTempNode.setSuccessor(newNode);
newNode.setPredecessor(preTempNode);
newNode.setSuccessor(tempNode);
tempNode.setPredecessor(newNode);
}
}
private void checkIndex(int i) throws IndexOutOfBoundsException {
if (i < 0 | i > size) {
throw new IndexOutOfBoundsException();
}
}
public boolean remove(int i) throws IndexOutOfBoundsException {
this.checkIndex(i);
if (getIndex(i) != null) {
getIndex(i).setPayload(null);
size--;
return true;
}
return false;
}
}

11
SimpleLinkedList/src/solution/SimpleLinkedListTest11111.java

@ -0,0 +1,11 @@
package solution;
public class SimpleLinkedListTest11111 extends SimpleListTest90 {
@Override
protected ISimpleList<String> getInstance1() {
// TODO Auto-generated method stub
return new SimpleLinkedList11111<String>();
}
}
Loading…
Cancel
Save