diff --git a/Collections2/.idea/.name b/Collections2/.idea/.name new file mode 100644 index 0000000..94271a5 --- /dev/null +++ b/Collections2/.idea/.name @@ -0,0 +1 @@ +MyLinkedList.java \ No newline at end of file diff --git a/Collections2/.idea/compiler.xml b/Collections2/.idea/compiler.xml index 4172516..cdee94f 100644 --- a/Collections2/.idea/compiler.xml +++ b/Collections2/.idea/compiler.xml @@ -10,7 +10,7 @@ - + \ No newline at end of file diff --git a/Collections2/.idea/misc.xml b/Collections2/.idea/misc.xml index 5755a99..484ded5 100644 --- a/Collections2/.idea/misc.xml +++ b/Collections2/.idea/misc.xml @@ -7,7 +7,7 @@ - + \ No newline at end of file diff --git a/Collections2/Collections2.iml b/Collections2/Collections2.iml index a0183d8..3310fd7 100644 --- a/Collections2/Collections2.iml +++ b/Collections2/Collections2.iml @@ -1,6 +1,6 @@ - + diff --git a/Collections2/pom.xml b/Collections2/pom.xml index 13f6e83..27ad92d 100644 --- a/Collections2/pom.xml +++ b/Collections2/pom.xml @@ -15,4 +15,8 @@ 5.7.1 + + 17 + 17 + \ No newline at end of file diff --git a/Collections2/src/main/java/itis/homework/impl/MyArrayList.java b/Collections2/src/main/java/itis/homework/impl/MyArrayList.java index 491dbc4..5214e69 100644 --- a/Collections2/src/main/java/itis/homework/impl/MyArrayList.java +++ b/Collections2/src/main/java/itis/homework/impl/MyArrayList.java @@ -1,7 +1,7 @@ -package itis.homework.impl; - -import java.util.List; - -public class MyArrayList implements List { - -} +//package itis.homework.impl; +// +//import java.util.List; +// +//public class MyArrayList implements List { +// +//} diff --git a/Collections2/src/main/java/itis/homework/impl/MyLinkedList.java b/Collections2/src/main/java/itis/homework/impl/MyLinkedList.java index 471390d..a61f842 100644 --- a/Collections2/src/main/java/itis/homework/impl/MyLinkedList.java +++ b/Collections2/src/main/java/itis/homework/impl/MyLinkedList.java @@ -1,7 +1,165 @@ package itis.homework.impl; -import java.util.List; +import java.util.*; public class MyLinkedList implements List { + private static class Node { + Object value; + Node previous; + Node next; + + public Node(Object value) { + this.value = value; + this.previous = null; + this.next = null; + } + + @Override + public boolean equals(Object o) { + return value.equals(o); + } + } + + Node head; + Node tail; + int size = 0; + + + public int size() { + return size; + } + + public boolean add(T t) { + if (head == null) { + head = new Node(t); + tail = head; + } else { + tail.next = new Node(t); + tail.next.previous = tail; + tail = tail.next; + } + size++; + return true; + } + + public boolean remove(Object o) { + if (isEmpty()) { + throw new NoSuchElementException(); + } + if (size == 1) { + clear(); + return true; + } + Node current = head; + while (!current.equals(o)) { + if(current.next == null) { + throw new NoSuchElementException(); + } + current = current.next; + } + if (current.value == null) { + throw new NoSuchElementException(); + } + Node pre = current.previous; + Node next = current.next; + + if (pre == null) { + head = next; + head.previous = null; + } else if(next == null) { + tail = pre; + tail.next = null; + } else { + pre.next = next; + next.previous = pre; + } + size--; + return true; + } + + public boolean isEmpty() { + if (this.head == null) { + return true; + } else return false; + } + + public T get(int index) { + Node current = head; + for (int i = 0; i < index; i++) { + current = current.next; + } + return (T) current.value; + } + public void clear() { + size = 0; + } + + public boolean contains(Object o) { + return false; + } + + public Iterator iterator() { + return null; + } + + public Object[] toArray() { + return new Object[0]; + } + + public T1[] toArray(T1[] a) { + return null; + } + + public boolean containsAll(Collection c) { + return false; + } + + public boolean addAll(Collection c) { + return false; + } + + public boolean addAll(int index, Collection c) { + return false; + } + + public boolean removeAll(Collection c) { + return false; + } + + public boolean retainAll(Collection c) { + return false; + } + + public void add(int index, T element) { + + } + + public T remove(int index) { + return null; + } + + public T set(int index, T element) { + return null; + } + + public int indexOf(Object o) { + return 0; + } + + public int lastIndexOf(Object o) { + return 0; + } + + public ListIterator listIterator() { + return null; + } + + public ListIterator listIterator(int index) { + return null; + } + + public List subList(int fromIndex, int toIndex) { + return null; + } } diff --git a/Collections2/src/test/java/HomeWorkCheck.java b/Collections2/src/test/java/HomeWorkCheck.java index 240eddc..16e4541 100644 --- a/Collections2/src/test/java/HomeWorkCheck.java +++ b/Collections2/src/test/java/HomeWorkCheck.java @@ -1,4 +1,4 @@ -import itis.homework.impl.MyArrayList; +import itis.homework.impl.MyLinkedList; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -13,7 +13,7 @@ static void setup() { @Test void check() { - List list = new MyArrayList(); + List list = new MyLinkedList(); list.add("One"); list.add("Two"); list.add("Three"); @@ -31,7 +31,7 @@ void check() { list.add("Fifteen"); Assertions.assertEquals(list.get(2), "Three"); - list.remove(2); + list.remove("Three"); Assertions.assertEquals(list.get(2), "Four"); } }