|
| 1 | +# 이중 연결 리스트 |
| 2 | + |
| 3 | +단일 연결 리스트는 선형으로 연결된 데이터 구조로, 노드로 구성되어 있다. 각 노드는 내용이 저장된 `data` 변수와 다음 노드를 가리키는 `pointer`로 구성되어 있다. 연결 리스트는 이런 노드들의 첫 원소를 가리키는 포인터를 가지며, 연산에 걸리는 시간을 훨씬 절약하기 위해 마지막 노드를 가리키는 포인터를 가질 수도 있다. 전체 노드 수를 저장하는 `length` 변수를 추가할 수도 있다. |
| 4 | + |
| 5 | +**이중 연결 리스트 (DLL)** 는 여기에 더해 이전 노드를 가리키는 `previous pointer`라는 추가적인 포인터도 가진다. |
| 6 | + |
| 7 | +### 단일 연결 리스트보다 우수한 점 |
| 8 | + |
| 9 | +- 정방향과 역방향 순회가 가능하다. |
| 10 | +- 제거될 노드를 가리키는 포인터가 알려져 있다면, 원소의 제거가 더 효율적이다. |
| 11 | +- 특정 노드 앞에 새로운 노드를 빠르게 삽입할 수 있다. |
| 12 | +- 단일 연결 리스트에서 노드를 제거하려면 이전 노드의 `pointer`를 알아야 한다. 어쩌면 이 노드를 찾기 위해 리스트를 전부 순회해야 할 수도 있다. 그러나 DLL에서는 `previous pointer`를 이용하여 이전 노드를 찾을 수 있다. |
| 13 | + |
| 14 | +### 단점 |
| 15 | + |
| 16 | +- 포인터를 저장하기 위한 메모리가 단일 연결 리스트보다 더 많이 필요하다. 그러나 포인터 하나로 DLL을 구현할 수도 있다. |
| 17 | +- 모든 작업에 추가적인 연산이 필요하다. 예를 들어, 삽입 시 `pointer`와 `previous pointer`를 모두 수정해야 한다. |
| 18 | + |
| 19 | +### 시간 복잡도 |
| 20 | + |
| 21 | +| 작업 | 평균 | 최악 | |
| 22 | +| ---- | ------ | ------ | |
| 23 | +| 접근 | $Θ(n)$ | $O(n)$ | |
| 24 | +| 탐색 | $Θ(n)$ | $O(n)$ | |
| 25 | +| 삽입 | $Θ(1)$ | $O(1)$ | |
| 26 | +| 제거 | $Θ(1)$ | $O(1)$ | |
| 27 | + |
| 28 | +## 예시 |
| 29 | + |
| 30 | +```java |
| 31 | +class LinkedList { |
| 32 | + |
| 33 | + Node head; // Pointer to the first element |
| 34 | + Node tail; // Optional. Points to the last element |
| 35 | + |
| 36 | + int length; // Optional |
| 37 | + |
| 38 | + class Node { |
| 39 | + int data; // Node data. Can be int, string, float, templates, etc |
| 40 | + Node next; // Pointer to the next node on the list |
| 41 | + Node prev; |
| 42 | + |
| 43 | + Node(int data) { |
| 44 | + this.data = data; |
| 45 | + } |
| 46 | + } |
| 47 | +``` |
| 48 | + |
| 49 | +- 맨 앞에 노드 추가하기 |
| 50 | +  |
| 51 | + |
| 52 | + ```java |
| 53 | + public void push(int new_data) { |
| 54 | + |
| 55 | + /* 1. allocate node |
| 56 | + 2. put in the data */ |
| 57 | + Node new_Node = new Node(new_data); |
| 58 | + |
| 59 | + /* 3. Make next of new node as head and previous as NULL */ |
| 60 | + new_Node.next = head; |
| 61 | + new_Node.prev = null; |
| 62 | + |
| 63 | + /* 4. change prev of head node to new node */ |
| 64 | + if (head != null) |
| 65 | + head.prev = new_Node; |
| 66 | + |
| 67 | + /* 5. move the head to point to the new node */ |
| 68 | + head = new_Node; |
| 69 | + } |
| 70 | + ``` |
| 71 | + |
| 72 | +- 주어진 노드 뒤에 노드 추가하기 |
| 73 | +  |
| 74 | + |
| 75 | + ```java |
| 76 | + public void InsertAfter(Node prev_Node, int new_data) { |
| 77 | + |
| 78 | + /*1. check if the given prev_node is NULL */ |
| 79 | + if (prev_Node == null) { |
| 80 | + System.out.println("The given previous node cannot be NULL "); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + /* 2. allocate node |
| 85 | + 3. put in the data */ |
| 86 | + Node new_node = new Node(new_data); |
| 87 | + |
| 88 | + /* 4. Make next of new node as next of prev_node */ |
| 89 | + new_node.next = prev_Node.next; |
| 90 | + |
| 91 | + /* 5. Make the next of prev_node as new_node */ |
| 92 | + prev_Node.next = new_node; |
| 93 | + |
| 94 | + /* 6. Make prev_node as previous of new_node */ |
| 95 | + new_node.prev = prev_Node; |
| 96 | + |
| 97 | + /* 7. Change previous of new_node's next node */ |
| 98 | + if (new_node.next != null) |
| 99 | + new_node.next.prev = new_node; |
| 100 | + } |
| 101 | + } |
| 102 | + ``` |
| 103 | + |
| 104 | +## 구현 |
| 105 | + |
| 106 | +- [Java](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/DoublyLinkedList.java) |
| 107 | +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/Data%20Structure/Doubly%20Linked%20List.cpp) |
| 108 | +- [Python](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/doubly_linked_list.py) |
| 109 | +- [Go](https://github.com/TheAlgorithms/Go/blob/master/data-structures/linked-list/double-linkedlist.go) |
| 110 | +- [Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/double_list.rb) |
| 111 | + |
| 112 | +## 영상 URL |
| 113 | + |
| 114 | +- [CS50](https://www.youtube.com/watch?v=FHMPswJDCvU) |
0 commit comments