Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions src/main/java/List/LinkedList/LinkedList_enduf7686.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package List.LinkedList;

import List.List;

public class LinkedList_enduf7686<E> implements List<E> {

private Node<E> head;
private Node<E> 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<E> 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<E> 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<E> 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<E> {

private E value;

private Node<E> prev;
private Node<E> next;

public Node(E value) {
this.value = value;
}
}
}
172 changes: 172 additions & 0 deletions src/test/java/List/LinkedList/LinkedListTest_enduf7686.java
Original file line number Diff line number Diff line change
@@ -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<Integer> factory = new LinkedListFactory<>();
private List<Integer> 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();
}
}