|
| 1 | +package com.codesquad.datastructurestudy.linkedlist; |
| 2 | + |
| 3 | +/** |
| 4 | + * This class implements a DoublyLinkedList. This is done using the classes |
| 5 | + * LinkedList and Link. |
| 6 | + * |
| 7 | + * A linked list is similar to an array, it holds values. However, |
| 8 | + * links in a linked list do not have indexes. With a linked list |
| 9 | + * you do not need to predetermine it's size as it grows and shrinks |
| 10 | + * as it is edited. This is an example of a double ended, doubly |
| 11 | + * linked list. Each link references the next link and the previous |
| 12 | + * one. |
| 13 | + * |
| 14 | + * 연결 리스트는 배열과 유사하며, 값을 가지고 있다. 그러나 연결 리스트의 링크에는 |
| 15 | + * 인덱스가 없다. 연결 리스트를 사용하면 편집할 때 크기가 커지거나 작아지는 것을 |
| 16 | + * 미리 파악할 필요가 없다. 이것은 이중 말단, 이중 연결 리스트의 예다. 각 링크는 |
| 17 | + * 다음 링크와 이전 링크를 참조한다. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +public class DoublyLinkedList { |
| 22 | + /** |
| 23 | + * Head refers to the front of the list |
| 24 | + * 헤드는 목록 앞쪽을 가리킨다 |
| 25 | + */ |
| 26 | + private Link head; |
| 27 | + |
| 28 | + /** |
| 29 | + * Tail refers to the back of the list |
| 30 | + * 테일은 목록 뒤쪽을 가리킨다 |
| 31 | + */ |
| 32 | + private Link tail; |
| 33 | + |
| 34 | + /** |
| 35 | + * Default Constructor |
| 36 | + */ |
| 37 | + public DoublyLinkedList() { |
| 38 | + head = null; |
| 39 | + tail = null; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructs a list containing the elements of the array |
| 44 | + * 배열의 요소를 포함하는 포함하는 리스트 구성 |
| 45 | + * |
| 46 | + * @param array the array whose elements are to be placed into this list |
| 47 | + * @throws NullPointerException if the specified collection is null |
| 48 | + * |
| 49 | + * array : 이 리스트에 요소를 넣을 배열 |
| 50 | + * 만약 지정된 콜렉션이 null이면 NullPointerException을 throws 해라 |
| 51 | + */ |
| 52 | + public DoublyLinkedList(int[] array) { |
| 53 | + if (array == null) throw new NullPointerException(); |
| 54 | + for (int i : array) { |
| 55 | + insertTail(i); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Insert an element at the head |
| 61 | + * 머리 부분에 element 삽입 |
| 62 | + * |
| 63 | + * @param x Element to be inserted |
| 64 | + * 삽입할 element x |
| 65 | + */ |
| 66 | + public void insertHead(int x) { |
| 67 | + Link newLink = new Link(x); // Create a new link with a value attached to it |
| 68 | + if (isEmpty()) // Set the first element added to be the tail |
| 69 | + tail = newLink; |
| 70 | + else |
| 71 | + head.previous = newLink; // newLink <-- currentHead(head) |
| 72 | + newLink.next = head; // newLink <--> currentHead(head) |
| 73 | + head = newLink; // newLink(head) <--> oldHead |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Insert an element at the tail |
| 78 | + * |
| 79 | + * @param x Element to be inserted |
| 80 | + * |
| 81 | + */ |
| 82 | + public void insertTail(int x) { |
| 83 | + Link newLink = new Link(x); |
| 84 | + newLink.next = null; // currentTail(tail) newLink --> |
| 85 | + if (isEmpty()) { // Check if there are no elements in list then it adds first element |
| 86 | + tail = newLink; |
| 87 | + head = tail; |
| 88 | + } else { |
| 89 | + tail.next = newLink; // currentTail(tail) --> newLink --> |
| 90 | + newLink.previous = tail; // currentTail(tail) <--> newLink --> |
| 91 | + tail = newLink; // oldTail <--> newLink(tail) --> |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Delete the element at the head |
| 97 | + * |
| 98 | + * @return The new head |
| 99 | + */ |
| 100 | + public Link deleteHead() { |
| 101 | + Link temp = head; |
| 102 | + head = head.next; // oldHead <--> 2ndElement(head) |
| 103 | + head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at lod head so will be removed |
| 104 | + if (head == null) |
| 105 | + tail = null; |
| 106 | + return temp; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Delete the element at the tail |
| 111 | + * |
| 112 | + * @return The new tail |
| 113 | + */ |
| 114 | + public Link deleteTail() { |
| 115 | + Link temp = tail; |
| 116 | + tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null |
| 117 | + tail.next = null; // 2ndLast(tail) --> null |
| 118 | + if (tail == null) { |
| 119 | + head = null; |
| 120 | + } |
| 121 | + return temp; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Delete the element from somewhere in the list |
| 126 | + * |
| 127 | + * @param x element to be deleted |
| 128 | + * @return Link deleted |
| 129 | + */ |
| 130 | + public void delete(int x) { |
| 131 | + Link current = head; |
| 132 | + |
| 133 | + while (current.value != x) { |
| 134 | + if (current != tail) { |
| 135 | + current = current.next; |
| 136 | + } else { |
| 137 | + throw new RuntimeException("The element to be deleted does not exist!"); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if (current == head) |
| 142 | + deleteHead(); |
| 143 | + else if (current == tail) |
| 144 | + deleteTail(); |
| 145 | + else { |
| 146 | + current.previous.next = current.next; |
| 147 | + current.next.previous = current.previous; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Inserts element and reorders |
| 153 | + * |
| 154 | + * @param x Element to be added |
| 155 | + */ |
| 156 | + public void insertOrdered(int x) { |
| 157 | + Link newLink = new Link(x); |
| 158 | + Link current = head; |
| 159 | + while (current != null && x > current.value) |
| 160 | + current = current.next; |
| 161 | + |
| 162 | + if (current == head) |
| 163 | + insertHead(x); |
| 164 | + |
| 165 | + else if (current == null) |
| 166 | + insertTail(x); |
| 167 | + |
| 168 | + else { |
| 169 | + newLink.previous = current.previous; |
| 170 | + current.previous.next = newLink; |
| 171 | + newLink.next = current; |
| 172 | + |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Returns true if list is empty |
| 178 | + * 리스트가 비어있으면 true를 반환한다 |
| 179 | + * |
| 180 | + * @return true if list is empty |
| 181 | + * |
| 182 | + */ |
| 183 | + public boolean isEmpty() { |
| 184 | + return (head == null); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Prints contents of the list |
| 189 | + * 리스트 내용 출력 |
| 190 | + */ |
| 191 | + public void display() { |
| 192 | + Link current = head; |
| 193 | + while (current != null) { |
| 194 | + current.displayLink(); |
| 195 | + current = current.next; |
| 196 | + } |
| 197 | + System.out.println(); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +class Link { |
| 202 | + /** |
| 203 | + * Value of node |
| 204 | + */ |
| 205 | + public int value; |
| 206 | + /** |
| 207 | + * This points to the link in front of the new link |
| 208 | + */ |
| 209 | + public Link next; |
| 210 | + /** |
| 211 | + * This points to the link behind the new link |
| 212 | + */ |
| 213 | + public Link previous; |
| 214 | + |
| 215 | + /** |
| 216 | + * Constructor |
| 217 | + * |
| 218 | + * @param value Value of node |
| 219 | + */ |
| 220 | + public Link(int value) { |
| 221 | + this.value = value; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Displays the node |
| 226 | + */ |
| 227 | + public void displayLink() { |
| 228 | + System.out.print(value + " "); |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Main Method |
| 233 | + * |
| 234 | + * @param args Command line arguments |
| 235 | + */ |
| 236 | + public static void main(String[] args) { |
| 237 | + DoublyLinkedList myList = new DoublyLinkedList(); |
| 238 | + myList.insertHead(13); |
| 239 | + myList.insertHead(7); |
| 240 | + myList.insertHead(10); |
| 241 | + myList.display(); |
| 242 | + |
| 243 | + myList.insertTail(11); |
| 244 | + myList.display(); |
| 245 | + |
| 246 | + myList.deleteTail(); |
| 247 | + myList.display(); |
| 248 | + |
| 249 | + myList.delete(7); |
| 250 | + myList.display(); |
| 251 | + |
| 252 | + myList.insertOrdered(23); |
| 253 | + myList.insertOrdered(67); |
| 254 | + myList.insertOrdered(3); |
| 255 | + myList.display(); |
| 256 | + } |
| 257 | +} |
0 commit comments