From 6f56999f328c28080529f21bb33a7e78094dc80c Mon Sep 17 00:00:00 2001 From: schrom01 Date: Wed, 28 Sep 2022 20:03:04 +0200 Subject: [PATCH] Solved Task 4 --- src/ch/zhaw/ads/ADS2_4_test.java | 3 +-- src/ch/zhaw/ads/MyList.java | 2 +- src/ch/zhaw/ads/MySortedList.java | 42 +++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/ch/zhaw/ads/MySortedList.java 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; + } + + +}