-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. Создать контейнер на базе связанного списка [#159 #180032]
- Loading branch information
Showing
3 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.j4j.collection; | ||
|
||
public interface SimpleLinked<E> extends Iterable<E> { | ||
void add(E value); | ||
|
||
E get(int index); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package ru.j4j.collection; | ||
|
||
import java.util.ConcurrentModificationException; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.Objects; | ||
|
||
public class SimpleLinkedList<E> implements SimpleLinked<E> { | ||
|
||
private int size; | ||
private int modCount; | ||
private Node<E> head; | ||
|
||
@Override | ||
public void add(E value) { | ||
Node<E> newNode = new Node<>(value, null); | ||
if (head == null) { | ||
head = newNode; | ||
} else { | ||
Node<E> current = head; | ||
while (current.next != null) { | ||
current = current.next; | ||
} | ||
current.next = newNode; | ||
} | ||
size++; | ||
modCount++; | ||
} | ||
|
||
|
||
@Override | ||
public E get(int index) { | ||
Objects.checkIndex(index, size); | ||
Node<E> current = head; | ||
for (int i = 0; i < index; i++) { | ||
current = current.next; | ||
} | ||
return current.item; | ||
} | ||
|
||
@Override | ||
public Iterator<E> iterator() { | ||
return new Iterator<>() { | ||
private Node<E> current = head; | ||
private final int expectedModCount = modCount; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (modCount != expectedModCount) { | ||
throw new ConcurrentModificationException(); | ||
} | ||
return current != null; | ||
} | ||
|
||
@Override | ||
public E next() { | ||
if (modCount != expectedModCount) { | ||
throw new ConcurrentModificationException(); | ||
} | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
E value = current.item; | ||
current = current.next; | ||
return value; | ||
} | ||
}; | ||
} | ||
|
||
private static class Node<E> { | ||
private final E item; | ||
private Node<E> next; | ||
|
||
Node(E element, Node<E> next) { | ||
this.item = element; | ||
this.next = next; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package ru.j4j.collection; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class SimpleLinkedListTest { | ||
|
||
private SimpleLinked<Integer> list; | ||
|
||
@BeforeEach | ||
public void initData() { | ||
list = new SimpleLinkedList<>(); | ||
list.add(1); | ||
list.add(2); | ||
} | ||
|
||
@Test | ||
void checkIteratorSimple() { | ||
assertThat(list).hasSize(2); | ||
list.add(3); | ||
list.add(4); | ||
assertThat(list).hasSize(4); | ||
} | ||
|
||
@Test | ||
void whenAddAndGet() { | ||
list.add(3); | ||
list.add(4); | ||
assertThat(list.get(0)).isEqualTo(1); | ||
assertThat(list.get(1)).isEqualTo(2); | ||
assertThat(list.get(2)).isEqualTo(3); | ||
assertThat(list.get(3)).isEqualTo(4); | ||
} | ||
|
||
@Test | ||
void whenGetFromOutOfBoundThenExceptionThrown() { | ||
assertThatThrownBy(() -> list.get(2)) | ||
.isInstanceOf(IndexOutOfBoundsException.class); | ||
} | ||
|
||
@Test | ||
void whenAddIterHasNextTrue() { | ||
Iterator<Integer> iterator = list.iterator(); | ||
assertThat(iterator.hasNext()).isTrue(); | ||
} | ||
|
||
@Test | ||
void whenAddIterNextOne() { | ||
Iterator<Integer> iterator = list.iterator(); | ||
assertThat(iterator.next()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
void whenEmptyIterHashNextFalse() { | ||
LinkedList<Integer> list = new LinkedList<>(); | ||
Iterator<Integer> iterator = list.iterator(); | ||
assertThat(iterator.hasNext()).isFalse(); | ||
} | ||
|
||
@Test | ||
void whenAddIterMultiHasNextTrue() { | ||
Iterator<Integer> iterator = list.iterator(); | ||
assertThat(iterator.hasNext()).isTrue(); | ||
assertThat(iterator.hasNext()).isTrue(); | ||
} | ||
|
||
@Test | ||
void whenAddIterNextOneNextTwo() { | ||
Iterator<Integer> iterator = list.iterator(); | ||
assertThat(iterator.next()).isEqualTo(1); | ||
assertThat(iterator.next()).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
void whenGetIteratorTwiceThenEveryFromBegin() { | ||
Iterator<Integer> first = list.iterator(); | ||
assertThat(first.hasNext()).isTrue(); | ||
assertThat(first.next()).isEqualTo(1); | ||
assertThat(first.hasNext()).isTrue(); | ||
assertThat(first.next()).isEqualTo(2); | ||
assertThat(first.hasNext()).isFalse(); | ||
Iterator<Integer> second = list.iterator(); | ||
assertThat(second.hasNext()).isTrue(); | ||
assertThat(second.next()).isEqualTo(1); | ||
assertThat(second.hasNext()).isTrue(); | ||
assertThat(second.next()).isEqualTo(2); | ||
assertThat(second.hasNext()).isFalse(); | ||
} | ||
} |