Skip to content

Commit 7541204

Browse files
committed
doubly linked list 구현
1 parent c13a50f commit 7541204

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package dev.idion.list.linkedlist.doubly;
2+
3+
/**
4+
* Doubly Linked List 구현체
5+
* @author dion
6+
* @version 1.0.0
7+
*/
8+
public class DoublyLinkedList {
9+
/**
10+
* head node
11+
*/
12+
private Node head;
13+
/**
14+
* tail node
15+
*/
16+
private Node tail;
17+
/**
18+
* List의 원소 수
19+
*/
20+
private int size;
21+
22+
/**
23+
* 기본 생성자
24+
*/
25+
public DoublyLinkedList() {
26+
this.head = null;
27+
this.tail = null;
28+
}
29+
30+
/**
31+
* 리스트가 비어있는지 반환
32+
*
33+
* @return 비어있다면 true / 비어있지 않다면 false 반환
34+
*/
35+
public boolean isEmpty() {
36+
return head == null;
37+
}
38+
39+
/**
40+
* 리스트에 있는 node의 개수를 반환
41+
*
42+
* @return 리스트의 node 개수
43+
*/
44+
public int size() {
45+
return size;
46+
}
47+
48+
/**
49+
* head node 위치에 node를 추가합니다.
50+
*
51+
* @param x 추가될 node에 들어갈 value
52+
*/
53+
public void insertHead(int x) {
54+
Node newNode = new Node(x); // 새로운 Node
55+
if (isEmpty()) {
56+
tail = newNode;
57+
} else {
58+
head.previous = newNode;
59+
}
60+
newNode.next = head;
61+
head = newNode;
62+
size++;
63+
}
64+
65+
/**
66+
* tail node 위치에 node를 추가합니다.
67+
* add로도 쓸 수 있습니다.
68+
*
69+
* @param x 추가될 node에 들어갈 value
70+
*/
71+
public void insertTail(int x) {
72+
Node newNode = new Node(x); // 새로운 Node
73+
newNode.next = null;
74+
if (isEmpty()) {
75+
tail = newNode;
76+
head = tail;
77+
} else {
78+
tail.next = newNode;
79+
newNode.previous = tail;
80+
tail = newNode;
81+
}
82+
size++;
83+
}
84+
85+
/**
86+
* Node를 추가합니다.
87+
*
88+
* @param x 추가될 node에 들어갈 value
89+
*/
90+
public void add(int x) {
91+
insertTail(x);
92+
}
93+
94+
/**
95+
* head node를 제거합니다.
96+
*
97+
* @return 제거된 node
98+
*/
99+
public Node deleteHead() {
100+
Node temp = head;
101+
head = head.next;
102+
head.previous = null; // temp와의 연결을 끊음
103+
System.out.println(head);
104+
if (head == null) {
105+
tail = null;
106+
}
107+
size--;
108+
return temp;
109+
}
110+
111+
/**
112+
* tail node를 제거합니다.
113+
*
114+
* @return 제거한 node
115+
*/
116+
public Node deleteTail() {
117+
Node temp = tail;
118+
tail = tail.previous;
119+
tail.next = null;
120+
if (tail == null) {
121+
head = null;
122+
}
123+
size--;
124+
return temp;
125+
}
126+
127+
/**
128+
* node를 제거합니다.
129+
*
130+
* @param node 제거할 node
131+
*/
132+
public void delete(Node node) {
133+
Node current = head;
134+
135+
while (current.value != node.value) {
136+
if (current != tail) {
137+
current = current.next;
138+
} else {
139+
throw new RuntimeException("그 노드는 없는데요?");
140+
}
141+
}
142+
143+
if (current == head) {
144+
deleteHead();
145+
} else if (current == tail) {
146+
deleteTail();
147+
} else {
148+
current.previous.next = current.next;
149+
current.next.previous = current.previous;
150+
size--;
151+
}
152+
}
153+
154+
/**
155+
* 리스트를 출력합니다.
156+
*/
157+
public void display() {
158+
Node current = head;
159+
while (current != null) {
160+
current.displayNode();
161+
current = current.next;
162+
}
163+
System.out.println();
164+
}
165+
}
166+
167+
/**
168+
* Doubly Linked List의 Node 구현체
169+
* @author dion
170+
* @version 1.0.0
171+
*/
172+
class Node {
173+
public int value;
174+
public Node next;
175+
public Node previous;
176+
177+
/**
178+
* Node 생성자
179+
*
180+
* @param value Node의 값
181+
*/
182+
public Node(int value) {
183+
this.value = value;
184+
}
185+
186+
/**
187+
* Node를 보여줍니다.
188+
*/
189+
public void displayNode() {
190+
System.out.print(value + " ");
191+
}
192+
193+
/**
194+
* Main Method
195+
*
196+
* @param args Command line arguments
197+
*/
198+
public static void main(String args[]) {
199+
DoublyLinkedList myList = new DoublyLinkedList();
200+
myList.insertHead(13);
201+
myList.insertHead(7);
202+
myList.insertHead(10);
203+
myList.display(); // <-- 10(head) <--> 7 <--> 13(tail) -->
204+
205+
myList.insertTail(11);
206+
myList.display(); // <-- 10(head) <--> 7 <--> 13 <--> 11(tail) -->
207+
208+
myList.deleteTail();
209+
myList.display(); // <-- 10(head) <--> 7 <--> 13(tail) -->
210+
211+
myList.delete(new Node(7));
212+
myList.display(); // <-- 10(head) <--> 13(tail) -->
213+
214+
myList.display(); // <-- 3(head) <--> 10 <--> 13 <--> 23 <--> 67(tail) -->
215+
}
216+
}

0 commit comments

Comments
 (0)