diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..901eaf0 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/main/java/List/LinkedList/LinkedList_enduf7686.java b/src/main/java/List/LinkedList/LinkedList_enduf7686.java new file mode 100644 index 0000000..a66e4c3 --- /dev/null +++ b/src/main/java/List/LinkedList/LinkedList_enduf7686.java @@ -0,0 +1,107 @@ +package List.LinkedList; + +import List.List; + +public class LinkedList_enduf7686 implements List { + + private Node head; + private Node tail; + + private int size; + + @Override + public void insert(E data) { + if (head == null) { + head = new Node<>(data); + tail = head; + } else { + tail.next = new Node<>(data); + tail.next.prev = tail; + tail = tail.next; + } + + size++; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return head == null && tail == null && size == 0; + } + + @Override + public boolean contains(E o) { + Node node = head; + + while (node != null) { + if (node.value.equals(o)) { + return true; + } + + node = node.next; + } + + return false; + } + + @Override + public E get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + Node node = head; + + for (int i = 0; i < index; i++) { + node = node.next; + } + + return node.value; + } + + @Override + public E remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + Node node = head; + + for (int i = 0; i < index; i++) { + node = node.next; + } + + if (node.prev != null) { + node.prev.next = node.next; + } + + if (node.next != null) { + node.next.prev = node.prev; + } + + size--; + + if (size == 0) { + head = null; + tail = null; + } + + return node.value; + } + + private static class Node { + + private E value; + + private Node prev; + private Node next; + + public Node(E value) { + this.value = value; + } + } +} diff --git a/src/test/java/List/LinkedList/LinkedListTest_enduf7686.java b/src/test/java/List/LinkedList/LinkedListTest_enduf7686.java new file mode 100644 index 0000000..52f7698 --- /dev/null +++ b/src/test/java/List/LinkedList/LinkedListTest_enduf7686.java @@ -0,0 +1,172 @@ +package List.LinkedList; + +import List.List; +import id.ID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static id.ID.*; +import static id.ID.name; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +public class LinkedListTest_enduf7686 { + + private final LinkedListFactory factory = new LinkedListFactory<>(); + private List list; + + @BeforeEach + void init() { + //깃허브 아이디 입력하면 각자 만든 리스트 객체 생성 + list = factory.createList(enduf7686); + } + + @Test + void LinkedList의_생성_및_초기화() { + assertThat(list).isNotNull(); + } + + @Test + void 데이터_5개_저장() { + list.insert(11); + list.insert(11); + list.insert(22); + list.insert(22); + list.insert(33); + + assertThat(list.size()).isEqualTo(5); + assertThat(list.get(0)).isEqualTo(11); + assertThat(list.get(1)).isEqualTo(11); + assertThat(list.get(2)).isEqualTo(22); + assertThat(list.get(3)).isEqualTo(22); + assertThat(list.get(4)).isEqualTo(33); + } + + + @Test + void 데이터_5개_저장_후_22인_데이터_모두_삭제() { + list.insert(11); + list.insert(11); + list.insert(22); + list.insert(22); + list.insert(33); + + // Iterable을 구현하면 enhanced for loop를 사용할 수 있습니다. + for (int i = 0; i < list.size(); i++) { + if (list.get(i) == 22) { + list.remove(i); + i--; // 삭제되면 index가 조정되므로 해당 index부터 다시 확인해야 합니다. + } + } + + assertThat(list.size()).isEqualTo(3); + assertThat(list.get(0)).isEqualTo(11); + assertThat(list.get(1)).isEqualTo(11); + assertThat(list.get(2)).isEqualTo(33); + } + + @Test + void 데이터_중복_저장_후_모두_포함여부_확인() { + list.insert(11); + list.insert(11); + list.insert(22); + list.insert(22); + list.insert(33); + + assertThat(list.contains(11)).isTrue(); + assertThat(list.contains(22)).isTrue(); + assertThat(list.contains(33)).isTrue(); + } + + @Test + void 비어있는_리스트_확인() { + assertThat(list.isEmpty()).isTrue(); + list.insert(10); + assertThat(list.isEmpty()).isFalse(); + } + + @Test + void 데이터_삽입_후_크기_확인() { + assertThat(list.size()).isEqualTo(0); + list.insert(10); + assertThat(list.size()).isEqualTo(1); + } + + @Test + void 유효하지_않은_인덱스로_데이터_가져오기() { + assertThatThrownBy(() -> list.get(0)).isInstanceOf(IndexOutOfBoundsException.class); + list.insert(10); + assertThatThrownBy(() -> list.get(-1)).isInstanceOf(IndexOutOfBoundsException.class); + assertThatThrownBy(() -> list.get(2)).isInstanceOf(IndexOutOfBoundsException.class); + } + + @Test + void 유효하지_않은_인덱스로_데이터_삭제() { + assertThatThrownBy(() -> list.remove(0)).isInstanceOf(IndexOutOfBoundsException.class); + list.insert(10); + assertThatThrownBy(() -> list.remove(-1)).isInstanceOf(IndexOutOfBoundsException.class); + assertThatThrownBy(() -> list.remove(2)).isInstanceOf(IndexOutOfBoundsException.class); + } + + @Test + void 모든_데이터_삭제_후_리스트_비우기() { + list.insert(10); + list.insert(20); + list.insert(30); + + list.remove(0); + list.remove(0); + list.remove(0); + + assertThat(list.size()).isEqualTo(0); + assertThat(list.isEmpty()).isTrue(); + } + + @Test + void 데이터_삽입_및_삭제_반복() { + list.insert(10); + list.insert(20); + list.insert(30); + list.insert(40); + + list.remove(1); + list.insert(50); + + assertThat(list.size()).isEqualTo(4); + assertThat(list.get(0)).isEqualTo(10); + assertThat(list.get(1)).isEqualTo(30); + assertThat(list.get(2)).isEqualTo(40); + assertThat(list.get(3)).isEqualTo(50); + } + + @Test + void 리스트의_중간에_데이터_삽입() { + list.insert(10); + list.insert(20); + list.insert(30); + + list.remove(1); + list.insert(25); + + assertThat(list.size()).isEqualTo(3); + assertThat(list.get(0)).isEqualTo(10); + assertThat(list.get(1)).isEqualTo(30); + assertThat(list.get(2)).isEqualTo(25); + } + + @Test + void 데이터_5개_삽입_후_모두_삭제() { + list.insert(10); + list.insert(20); + list.insert(30); + list.insert(40); + list.insert(50); + + for (int i = 0; i < 5; i++) { + list.remove(0); + } + + assertThat(list.size()).isEqualTo(0); + assertThat(list.isEmpty()).isTrue(); + } +} \ No newline at end of file