Solved Task 4

This commit is contained in:
schrom01 2022-09-28 20:03:04 +02:00
parent 6c80c7ae06
commit 6f56999f32
3 changed files with 44 additions and 3 deletions

View File

@ -16,7 +16,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* @author * @author
* @version 1.00 2017/8/30 * @version 1.00 2017/8/30
*/ */
/**
public class ADS2_4_test { public class ADS2_4_test {
MySortedList list; MySortedList list;
@ -79,4 +79,3 @@ public class ADS2_4_test {
} }
} }
} }
*/

View File

@ -133,7 +133,7 @@ public class MyList extends AbstractList {
throw new UnsupportedOperationException("Method not implemented!"); throw new UnsupportedOperationException("Method not implemented!");
} }
private class MyNode { public class MyNode {
private MyNode nextNode; private MyNode nextNode;
private MyNode previousNode; private MyNode previousNode;
private Object data; private Object data;

View File

@ -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;
}
}