Skip to content

Commit

Permalink
2. Создать контейнер на базе связанного списка [#159 #180032]
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzor committed Jul 6, 2024
1 parent 08f88f9 commit 6618853
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/ru/j4j/collection/SimpleLinked.java
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);
}
79 changes: 79 additions & 0 deletions src/main/java/ru/j4j/collection/SimpleLinkedList.java
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;
}
}
}
95 changes: 95 additions & 0 deletions src/test/java/ru/j4j/collection/SimpleLinkedListTest.java
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();
}
}

0 comments on commit 6618853

Please sign in to comment.