Post

Doubly LinkedList

Doubly LinkedList 구현

st-lab 님의 블로그를 참고해서 구현한 Doubly LinkedList이다.

Node< E >

1
2
3
4
5
6
7
8
9
10
11
public class Node<E> {
  E data;
  Node<E> next;
  Node<E> prev;

  Node<E> (E data) {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}

Singly LinkedList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;

public class DoublyLinkedList<E> implements Interface_form.List<E> {

  private Node<E> head;
  private Node<E> tail;
  private int size;

  public DoublyLinkedList() {
    this.head = null;
    this.tail = null;
    this.size = 0;
  }

  public Node<E> search(int index) {
    if (index < 0 || index >= size) {
      throw new IndexOutOfBoundsException();
    }
    // if ((size / 2) < index) { // 뒤에서부터 탐색
    //   for (Node<E> x = tail; x != null; x = x.prev) {
    //     int i = size - 1;
    //     if (i-- == index) {
    //       return x;
    //     }
    //   }
    // } else {
    //   for (Node<E> x = head; x != null; x = x.next) {
    //     int i = 0;
    //     if (i++ == index) {
    //       return x;
    //     }
    //   }
    // }
    // 너무 Node를 이용한 조건문 사용에 매몰된 것 같다. 그리고 size도 인덱스보다 1 많으니 조건문 설정할 때 index + 1을 해줘야 정확한 비교 가능
    if (size / 2 < index + 1) {
      Node<E> x = tail;
      for (int i = size-1; i != index; i--) {
        x = x.prev;
      }
    } else {
      Node<E> x = head;
      for (int i = 0; i != index; i++) {
        x = x.next;
      }
    }
    return x;
  }

  public void addFirst(E value) {
    Node<E> addNode = new Node<E>(value);
    // if (head == null) {// 노드에 아무것도 없을 때
    //   head = addNode;
    //   tail = addNode;
    //   return;
    // }
    // Node<E> nextNode = head;
    // addNode.next = nextNode;
    // nextNode.prev = addNode;
    // head = addNode; 
    // 더 효율적으로 짜보자
    addNode.next = head;
    if (head != null) {
      head.prev = addNode;
    }
    head = addNode;
    size++;
    if (head == null) {
      tail = addNode;
    }
  }

  @Override
  public boolean add(E value) {
    addLast(value);
    return true;
  }

  public void addLast(E value) {
    Node<E> newNode = new Node<E>(value);
    if (tail == null) {
      addFirst(value);
      return;
    }
    newNode.prev = tail;
    tail.next = newNode;
    tail = newNode;
    size++;
  }

  public void add(int index, E value) {
    if (index < 0 || index >= size) {
      throw new IndexOutOfBoundsException();
    }
    if (index == 0) {
      addFirst(value);
      return;
    }
    if (index == size - 1) {
      addLast(value);
      return;
    }
    Node<E> newNode = new Node<E>(value);
    Node<E> preNode = search(index);
    Node<E> nxtNode = preNode.next;

    preNode.next = newNode;
    nxtNode.prev = newNode;
    newNode.prev = preNode;
    newNode.next = nxtNode;
    size++;
  }

  public E remove() {
    if (head = null) {
      throw new NoSuchElementException();
    }
    Node<E> rmNode = head;
    Node<E> nxtNode;
    if (rmNode.next != null) {
      nxtNode = rmNode.next;
      nxtNode = null; 
    }
    E element = rmNode.data;
    rmNode.data = null;
    rmNode.next = null;
    // rmNode.prev = null; // head를 가져왔기 때문에 이미 null이다.
    head = nxtNode;
    size--;
    // node가 하나만 있었을 경우 설정 또 깜빡함
    if (size == 0) {
      tail = null;
    }
    return element;
  }

  @Override
  public E remove(int index) {
    if (index < 0 || index >= size) {
      throw new IndexOutOfBoundsException();
    }
    if (index == 0) {
      return remove();
    }
    Node<E> delNode = search(index);
    Node<E> nxtNode = delNode.next;
    Node<E> preNode = delNode.prev;

    E element = delNode.data;
    delNode.data = null;
    delNode.next = null;
    delNode.prev = null;
    preNode.next = nxtNode;

    if (nxtNode != null) {
      nxtNode.prev = preNode;
    } else {
      tail = preNode;
    }
    size--;
    return element;
  }

  @Override
  public boolean remove(Object value) {
    if (head.data == value) {
      remove();
      return true;
    }
    Node<E> x = head;
    while (x.data != value) {
      x = x.next;
    }
    if (x == null) {
      return false;
    }
    Node<E> preNode = x.prev;
    Node<E> nxtNode = x.next;
    x.data = null;
    x.next = null;
    x.prev = null;
    preNode.next = nxtNode;
    if (nxtNode != null) {
      nxtNode.prev = preNode;
    } else {
      tail = preNode;
    }
    size--;
    return true;
  }

  @Override
  public E get(int index) {
    return search(index).data;
  }

  @Override
  public void set(int index, E value) {
    search(index).data = value;
  }

  @Override
  public int indexOf(Object value) {
    int index = 0;
    for (Node<E> x = head; x != null; x = x.next) {
      if (x.equals(value)) {
        return index;
      }
      index++;
    }
    return -1;
  }

  public int LastindexOf(Object value) {
    int index = size-1;
    for (Node<E> x = tail; x != null; x = x.prev) {
      if (x.equals(value)) {
        return index;
      }
      index--;
    }
    return -1;
  }

  @Override
  public boolean contains(Object item) {
    return indexOf(item) >= 0;
  }

  @Override
  public int size() {
    return size;
  }

  @Override
  public boolean isEmpty() {
    return size == 0;
  }

  @Override
  public void clear() {
    for (Node<E> x = head; x != null; x = x.next) {
      Node<E> tempNode = x.next;
      x.data = null;
      x.prev = null;
      x.next = null;
      x = tempNode;
    }
    head = tail = null;
    size = 0;
  }

  public Object clone() throws CloneNotSupportedException {

    @SuppressWarnings("unchecked")
    DoublyLinkedList<? super E> clone = (DoublyLinkedList<? super E>) super.clone();

    clone.head = null;
    clone.tail = null;
    clone.size = 0;

    for(Node<E> x = head; x != null; x = x.next) {
      clone.addLast(x.data);
    }
    return clone;
  }

  public Object[] toArray() {
    Object[] arr = new Object[size];
    int i = 0;
    for(Node<E> x = head; x != null; x = x.next) {
      arr[i++] = x.data;
    }
    return arr;
  }

  @SuppressWarnings("unchecked")
  public <T> T[] toArray(T[] a) {
    if (a.length < size) {
      a = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
      // a 사이즈르 재정의 하기 위해 a의 타입을 받고 사이즈 까지 받아 새로운 array 객체를 만든다.
    }
    int i = 0;
    Object[] result = a;
    for(Node<E> x = head; x != null; x = x.next) {
      result[i++] = x.data;
    }
    return result;
  }

  public void sort() {
    sort(null);
  }

  @SuppressWarnings({"unchecked", "rawtypes"})
  public void sort(Comparator<? super E> c) {
    Object[] a = this.toArray();
    Arrays.sort(a, (Comparator) c);
    int i = 0;
    for(Node<E> x = head; x != null; x = x.next) {
      x.data = (E) a[i++];
    }
  }
}

소스 코드 링크

DoublyLinkedList

출처) 자바 [JAVA] - Doubly LinkedList 구현하기

This post is licensed under CC BY 4.0 by the author.