From 6c80c7ae061dde2b73dd57714ef73be9b67ae814 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 27 Sep 2022 16:57:48 +0200 Subject: [PATCH] Solved Task 3 --- src/ch/zhaw/ads/ADS2_3_test.java | 3 +- src/ch/zhaw/ads/MyList.java | 171 +++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 src/ch/zhaw/ads/MyList.java diff --git a/src/ch/zhaw/ads/ADS2_3_test.java b/src/ch/zhaw/ads/ADS2_3_test.java index 82c2e99..831b6bf 100644 --- a/src/ch/zhaw/ads/ADS2_3_test.java +++ b/src/ch/zhaw/ads/ADS2_3_test.java @@ -15,7 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; * @author * @version 1.00 2017/8/30 */ -/** + public class ADS2_3_test { MyList list; @@ -98,4 +98,3 @@ public class ADS2_3_test { } } } -*/ \ No newline at end of file diff --git a/src/ch/zhaw/ads/MyList.java b/src/ch/zhaw/ads/MyList.java new file mode 100644 index 0000000..b1a6b55 --- /dev/null +++ b/src/ch/zhaw/ads/MyList.java @@ -0,0 +1,171 @@ +package ch.zhaw.ads; + +import java.util.AbstractList; +import java.util.Comparator; +import java.util.Spliterator; +import java.util.function.Consumer; +import java.util.function.IntFunction; +import java.util.function.Predicate; +import java.util.function.UnaryOperator; +import java.util.stream.Stream; + +public class MyList extends AbstractList { + MyNode head; + MyNode tail; + + @Override + public boolean add(Object o) { + if(head == null) + { + head = new MyNode(null, null, o); + tail = head; + } else { + MyNode actualNode = head; + while (actualNode.getNextNode() != null) { + actualNode = actualNode.getNextNode(); + } + actualNode.setNextNode(new MyNode(null, actualNode, o)); + tail = actualNode.getNextNode(); + } + return true; + } + + @Override + public boolean remove(Object o){ + MyNode actualNode = head; + while(actualNode != null) { + if(actualNode.getData() == o){ + MyNode previousNode = actualNode.getPreviousNode(); + MyNode nextNode = actualNode.getNextNode(); + + if(previousNode == null) { + head = nextNode; + } else { + previousNode.setNextNode(nextNode); + } + + if(nextNode == null) { + tail = previousNode; + } else { + nextNode.setPreviousNode(previousNode); + } + return true; + } + actualNode = actualNode.getNextNode(); + } + return false; + } + + @Override + public Object get(int index) { + MyNode actualNode = head; + for(int i = 0; i < index; i++) { + if(actualNode != null) { + actualNode = actualNode.nextNode; + } else { + throw new IndexOutOfBoundsException(); + } + } + return actualNode.getData(); + } + + @Override + public boolean isEmpty() { + return head == null; + } + + @Override + public void clear(){ + head = null; + tail = null; + } + + @Override + public void forEach(Consumer action) { + throw new UnsupportedOperationException("Method not implemented!"); + } + + @Override + public Spliterator spliterator() { + throw new UnsupportedOperationException("Method not implemented!"); + } + + @Override + public Stream stream() { + throw new UnsupportedOperationException("Method not implemented!"); + } + + @Override + public Stream parallelStream() { + throw new UnsupportedOperationException("Method not implemented!"); + } + + @Override + public int size() { + if(head == null){ + return 0; + } + MyNode actualNode = head; + int count = 1; + for(; actualNode.getNextNode() != null; count++){ + actualNode = actualNode.getNextNode(); + } + return count; + } + + @Override + public boolean removeIf(Predicate filter) { + throw new UnsupportedOperationException("Method not implemented!"); + } + + @Override + public Object[] toArray(IntFunction generator) { + throw new UnsupportedOperationException("Method not implemented!"); + } + + @Override + public void replaceAll(UnaryOperator operator) { + throw new UnsupportedOperationException("Method not implemented!"); + } + + @Override + public void sort(Comparator c) { + throw new UnsupportedOperationException("Method not implemented!"); + } + + private class MyNode { + private MyNode nextNode; + private MyNode previousNode; + private Object data; + + MyNode(MyNode nextNode, MyNode previousNode, Object data){ + setNextNode(nextNode); + setPreviousNode(previousNode); + setData(data); + } + + public void setNextNode(MyNode nextNode) { + this.nextNode = nextNode; + } + + public MyNode getNextNode() { + return nextNode; + } + + public void setPreviousNode(MyNode previousNode) { + this.previousNode = previousNode; + } + + public MyNode getPreviousNode() { + return previousNode; + } + + public void setData(Object data) { + this.data = data; + } + + public Object getData() { + return data; + } + } +}