LinkedList (링크드리스트)
1. 연결 리스트 (Linked List)
- 데이터를 링크로 연결해서 관리하는 자료구조
- 자료의 순서는 정해져 있지만, 메모리상 연속성이 보장되지는 않음
2. 연결 리스트의 장점
- 데이터 공간을 미리 할당할 필요 없음
- 즉, 리스트의 길이가 가변적이라 데이터 추가/삭제 용이
3. 연결 리스트의 단점
- 연결구조를 위한 별도 데이터 공간 필요
- 연결 정보를 찾는 시간이 필요 (접근 속도가 상대적으로 느림)
- 데이터 추가, 삭제 시 앞뒤 데이터의 연결을 재구성하는 작업 필요
4. 연결 리스트 기본 구조
- 노드 (Node)
- 데이터 저장 단위로, 값과 포인터로 구성
5. 연결 리스트 기본 연산
1) 데이터 추가 - 가장 앞에 추가
2) 데이터 추가 - 가장 뒤에 추가
3) 데이터 추가 - 중간에 추가
4) 데이터 삭제 - 가장 앞을 삭제
5) 데이터 삭제 - 가장 뒤를 삭제
6) 데이터 삭제 - 중간을 삭제
(실습 : 연결 리스트)
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
// 단순 연결 리스트 (simple ver.) 기본 구조 구현
// 노드
class Node {
int data;
Node next;
Node() {
}
Node(int data, Node next) {
this.data = data;
this.next = next;
}
}
class LinkedList {
Node head;
LinkedList() {}
LinkedList(Node node) {
this.head = node;
}
// 연결 리스트 비어있는지 확인
public boolean isEmpty() {
if (this.head == null) {
return true;
}
return false;
}
// 연결 리스트의 맨 뒤에 데이터 추가
public void addData(int data) {
if (this.head == null) {
this.head = new Node (data, null);
} else {
Node cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = new Node(data, null);
}
}
// 연결 리스트의 맨 뒤의 데이터 삭제
public void removeData() {
if (this.isEmpty()) {
System.out.println("List is Empty");
return;
}
Node cur = this.head;
Node prev = cur;
while (cur.next != null) {
prev = cur;
cur = cur.next;
}
if (cur == this.head) {
this.head = null;
} else {
prev.next = null;
}
}
// 연결 리스트에서 데이터 찾기
public void findData(int data) {
if (this.isEmpty()) {
System.out.println("List is Empty");
return;
}
Node cur = this.head;
while (cur != null) {
if (cur.data == data) {
System.out.println("Data exists!");
return;
}
cur = cur.next;
}
System.out.println("Data not found!");
}
// 연결 리스트의 모든 데이터 출력
public void showData() {
if (this.isEmpty()) {
System.out.println("List is Empty");
return;
}
Node cur = this.head;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
// Test Code
LinkedList myList = new LinkedList(new Node(1, null));
myList.showData(); // 1
myList.addData(2);
myList.addData(3);
myList.addData(4);
myList.addData(5);
myList.showData(); // 1 2 3 4 5
myList.findData(3); // Data exist!
myList.findData(100); // Data not found!
myList.removeData();
myList.removeData();
myList.removeData();
myList.showData(); // 1 2
myList.removeData();
myList.removeData();
myList.removeData(); // List is empty
}
}
This post is licensed under CC BY 4.0 by the author.