From 6a57d695244bbb17acbc272ee0f94db65b3d0dcc Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 16 Nov 2018 16:39:58 +0100 Subject: [PATCH] B2: remove & indexOf implementiert --- .../algodat/praktikum/blatt2/ArrayList.java | 5 +++ .../algodat/praktikum/blatt2/LinkedList.java | 5 +++ .../algodat/praktikum/blatt2/List.java | 1 + .../algodat/praktikum/blatt2/RingList.java | 37 +++++++++++++++++-- .../studium/algodat/vorlesung/Blatt2Test.java | 33 +++++++++++++++-- 5 files changed, 74 insertions(+), 7 deletions(-) diff --git a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/ArrayList.java b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/ArrayList.java index 670df78..5d3b5cd 100644 --- a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/ArrayList.java +++ b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/ArrayList.java @@ -49,6 +49,11 @@ public class ArrayList implements List { return false; } + @Override + public int indexOf(E e) { + return 0; + } + @NotNull public Iterator iterator() { return new Iterator() { diff --git a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/LinkedList.java b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/LinkedList.java index 6f7def8..94daa53 100644 --- a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/LinkedList.java +++ b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/LinkedList.java @@ -82,6 +82,11 @@ public class LinkedList implements List { return false; } + @Override + public int indexOf(E e) { + return 0; + } + @NotNull public Iterator iterator() { return new Iterator() { diff --git a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/List.java b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/List.java index 4038ce3..7ff2ec8 100644 --- a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/List.java +++ b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/List.java @@ -7,4 +7,5 @@ public interface List extends Iterable{ int size(); E remove(int index); boolean remove(Object o); + int indexOf(E e); } diff --git a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/RingList.java b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/RingList.java index 860c3af..273863e 100644 --- a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/RingList.java +++ b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/RingList.java @@ -35,7 +35,7 @@ public class RingList implements List { return true; } - public E get(int index) { + private Wrapper getWrapper(int index) { Wrapper wRun; if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); @@ -44,7 +44,11 @@ public class RingList implements List { for (int iRun = 0; iRun < index; iRun++) { wRun = wRun.succ; } - return wRun.e; + return wRun; + } + + public E get(int index) { + return getWrapper(index).e; } public int size() { @@ -53,14 +57,41 @@ public class RingList implements List { @Override public E remove(int index) { - return null; + Wrapper wrapper = getWrapper(index - 1); + wrapper.succ = wrapper.succ.succ; + size--; + + return wrapper.succ.e; } @Override public boolean remove(Object o) { + + try { + int index = indexOf((E) o); + if(index == -1) return false; + + remove(index); + return true; + } catch (ClassCastException ex) { + ex.printStackTrace(); + } + return false; } + @Override + public int indexOf(E e) { + Wrapper wrapper = this.first.succ; + for (int i = 0; i < size; i++) { + if(wrapper.e.equals(e)) { + return i; + }else wrapper = wrapper.succ; + } + return -1; + } + + @NotNull public Iterator iterator() { return new Iterator() { diff --git a/src/test/java/xyz/joethei/studium/algodat/vorlesung/Blatt2Test.java b/src/test/java/xyz/joethei/studium/algodat/vorlesung/Blatt2Test.java index 994bf63..3b0b3a8 100644 --- a/src/test/java/xyz/joethei/studium/algodat/vorlesung/Blatt2Test.java +++ b/src/test/java/xyz/joethei/studium/algodat/vorlesung/Blatt2Test.java @@ -1,6 +1,5 @@ package xyz.joethei.studium.algodat.vorlesung; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import xyz.joethei.studium.algodat.praktikum.blatt2.List; import xyz.joethei.studium.algodat.praktikum.blatt2.RingList; @@ -8,6 +7,7 @@ import xyz.joethei.studium.algodat.praktikum.blatt2.RingList; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; class Blatt2Test { @@ -39,17 +39,42 @@ class Blatt2Test { assertEquals(10, rList.size()); } - @Disabled + @Test - void remove() { + void removeIndex() { List rList = new RingList<>(); for (int i = 1; i <= 10; i++) { rList.add(i * i); } - assertEquals(Optional.of(36), Optional.ofNullable(rList.remove(5))); + assertEquals(Optional.of(49), Optional.ofNullable(rList.remove(5))); assertEquals(9, rList.size()); assertTrue(rList.get(5) != 36); } + @Test + void removeObject() { + List rList = new RingList<>(); + for (int i = 1; i <= 10; i++) { + rList.add(i * i); + } + + assertTrue(rList.remove(Integer.valueOf(9))); + assertFalse(rList.remove(Integer.valueOf(5))); + assertEquals(9, rList.size()); + assertEquals(-1, rList.indexOf(9)); + } + + @Test + void indexOf() { + List rList = new RingList<>(); + for (int i = 1; i <= 10; i++) { + rList.add(i * i); + } + assertEquals(0, rList.indexOf(1)); + assertEquals(1, rList.indexOf(4)); + assertEquals(2, rList.indexOf(9)); + assertEquals(3, rList.indexOf(16)); + } + } \ No newline at end of file