diff --git a/src/ch/zhaw/ads/ADS2_4_test.java b/src/ch/zhaw/ads/ADS2_4_test.java index 647e4e8..1748c58 100644 --- a/src/ch/zhaw/ads/ADS2_4_test.java +++ b/src/ch/zhaw/ads/ADS2_4_test.java @@ -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 { } } } -*/ \ No newline at end of file diff --git a/src/ch/zhaw/ads/MyList.java b/src/ch/zhaw/ads/MyList.java index b1a6b55..8edfe78 100644 --- a/src/ch/zhaw/ads/MyList.java +++ b/src/ch/zhaw/ads/MyList.java @@ -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; diff --git a/src/ch/zhaw/ads/MySortedList.java b/src/ch/zhaw/ads/MySortedList.java new file mode 100644 index 0000000..624399a --- /dev/null +++ b/src/ch/zhaw/ads/MySortedList.java @@ -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; + } + + +}