Solved Task 4
This commit is contained in:
parent
6c80c7ae06
commit
6f56999f32
|
@ -16,7 +16,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
* @author
|
||||
* @version 1.00 2017/8/30
|
||||
*/
|
||||
/**
|
||||
|
||||
public class ADS2_4_test {
|
||||
MySortedList list;
|
||||
|
||||
|
@ -79,4 +79,3 @@ public class ADS2_4_test {
|
|||
}
|
||||
}
|
||||
}
|
||||
*/
|
|
@ -133,7 +133,7 @@ public class MyList extends AbstractList {
|
|||
throw new UnsupportedOperationException("Method not implemented!");
|
||||
}
|
||||
|
||||
private class MyNode {
|
||||
public class MyNode {
|
||||
private MyNode nextNode;
|
||||
private MyNode previousNode;
|
||||
private Object data;
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package ch.zhaw.ads;
|
||||
|
||||
public class MySortedList extends MyList {
|
||||
|
||||
@Override
|
||||
public boolean add(Object o) {
|
||||
if (!(o instanceof Comparable)) {
|
||||
throw new IllegalArgumentException("Object is not comparable!");
|
||||
}
|
||||
if (head == null) {
|
||||
head = new MyNode(null, null, o);
|
||||
tail = head;
|
||||
} else {
|
||||
Comparable newData = (Comparable)o;
|
||||
MyNode actual_Node = head;
|
||||
while(newData.compareTo(actual_Node.getData()) > 0){
|
||||
if(actual_Node.getNextNode() != null){
|
||||
actual_Node = actual_Node.getNextNode();
|
||||
} else {
|
||||
//insert after actual
|
||||
MyNode newNode = new MyNode(null, actual_Node, o);
|
||||
actual_Node.setNextNode(newNode);
|
||||
tail = newNode;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//insert before actual
|
||||
MyNode newNode = new MyNode(actual_Node, actual_Node.getPreviousNode(), o);
|
||||
if(actual_Node.getPreviousNode() != null) {
|
||||
actual_Node.getPreviousNode().setNextNode(newNode);
|
||||
}
|
||||
actual_Node.setPreviousNode(newNode);
|
||||
if(actual_Node == head)
|
||||
{
|
||||
head = newNode;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue