LinkedList ์ ๊ฐ๋
ArrayList
- ๋ฐฐ์ด์ ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ๋ค.
- ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๊ฑฐ๋ ์ถ๊ฐํ๋ฉด ๋ ํฐ ๊ณต๊ฐ์ผ๋ก (๋๋ ๋ ์์ ๊ณต๊ฐ์ผ๋ก) ์ด๋ํด์ผ ํ๋ค. (์๊ฐ์ด ๋ง์ด ์์๋จ)
- ๋ง์ฝ ํฌ๊ธฐ๊ฐ 5์ธ list์ ์ค๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ฉด ๊ทธ ๋ค์์ ์๋ ๋ฐ์ดํฐ๋ค์ ํ์นธ ์ฉ ๋ชจ๋ ์ด๋์์ผ์ผ ํ๋ฏ๋ก (๋ง์ฝ ํฌ๊ธฐ๊ฐ 1์ต์ด๋ฉด? ์์ฒญ๋๋ค.)
- ์ํ๋ ์ธ๋ฑ์ค์ ๋ฐ๋ก ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
LinkedList
- ์ฐ๊ฒฐ(link)๋ ๊ตฌ์กฐ์ด๋ค.
- ArrayList์ ๊ฐ์ฅ ํฐ ์ฐจ์ด์ ์ด ๋ฐ์ดํฐ๋ค์ด ์ฌ๊ธฐ์ ๊ธฐ ํฉ์ด์ ธ ์๋ค.
- ์ฅ์ :
- ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ ๋, ์ด์ ๊ฐ๊ณผ ์ถ๊ฐ(๋๋ ์ญ์ )ํ ๊ฐ์ next๋ง ๋ณ๊ฒฝํ๋ฉด ๋๋ฏ๋ก ํจ์ฌ ๋น ๋ฅด๋ค.
- ๋จ์ :
- ์ํ๋ ์ธ๋ฑ์ค์ ์ง์ ์ ๊ทผ์ด ์๋๋ค.
- ๋ ธ๋์๋ [๋ฐ์ดํฐ + ๋ค์ ๋ ธ๋] ๊ฐ ๋ค์ด์๊ธฐ ๋๋ฌธ์ head์์ ๋ถํฐ ๋ค์ ๋ ธ๋๋ฅผ ์ฐพ๊ณ ์ฐพ์์ ํด๋น ์ธ๋ฑ์ค๊น์ง ์ด๋ํด์ ์ฐพ์์ผํ๋ฏ๋ก ArrayList์ ๋นํด ํจ์ฌ ๋๋ฆฌ๋ค.
- ์ฅ์ :
LinkedList์ ๊ตฌ์กฐ
arrayList๋ ์๋ฆฌ๋จผํธ๋ผ๊ณ ๋ถ๋ ์ง๋ง, LinkedList์์๋ ๋ ธ๋(node) ๋ผ๊ณ ๋ถ๋ฅธ๋ค. ๋ค๋ฅธ ์ฉ์ด๋ก๋ ์ ์ (vertex)์ด ์๋ค.
๋ ธ๋๋ ๋๊ฐ์ง ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์์ด์ผ ํ๋ค.
- ๋ ธ๋์ ๊ฐ
- ๋ค์ ๋ ธ๋
๋ ธ๋๋ฅผ ์ฒ์์์น์ ์ถ๊ฐ
https://visualgo.net/en/list
์ ์ฌ์ดํธ์ ๊ฐ๋ฉด, ๋ฆฌ์คํธ์ ํธ๋ฆฌ ๋ฑ๋ฑ ๋ค๋ฅธ ๊ตฌ์กฐ์ ๋ํ ์ดํด๋ฅผ ๊ทธ๋ฆผ์ผ๋ก ์ ์ค๋ช ํด ๋์ ์ฌ์ดํธ ์ด๋ค. ์ฐธ๊ณ ํ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค.
์ฝ์ ํ๋ ค๋ ๋ ธ๋๋ฅผ head๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ํ ๋ค์,
Vertex newNode = new Vertex(85); // 85 ์์ฑ
newNode.next = head; // 85์ next๋ฅผ ํ์ฌ head๋ก ์ค์ ํ๋ค.
head = newNode; // ๊ทธ๋ฆฌ๊ณ 85๋ฅผ head๋ก ์ค์ ํ๋ค.
์๋ head ์๋ ๋ ธ๋๋ฅผ ์ฝ์ ํ๋ ค๋ ๋ ธ๋๋ก ๋ฐ๊พผ๋ค.
๋ ธ๋๋ฅผ ์ค๊ฐ์์น์ ์ถ๊ฐ
์๋ก์ด ๋ ธ๋ 90์ index=2์ธ 23์ ์๋ฆฌ์ ์ถ๊ฐํ๊ณ ์ถ๋ค. ์ด๋ป๊ฒ ํด์ผ ํ ๊น?
- ๋จผ์ 6๊ณผ 23์ ์ฐพ๋๋ค.
- 6์ next๋ฅผ 90์ผ๋ก
- 90์ next๋ฅผ 23์ผ๋ก ์ค์
Vertex newNode = new Vertex(90); // ์๋ก์ด ๋
ธ๋
// 6 (23์ ์ด์ ๋
ธ๋)์ ์์น๋ฅผ ๋จผ์ ์ฐพ๋๋ค. index = 2;
Vertex previous = head;
for(int i=0; i<1; i++) {
previous = previous.next;
}
//---------------------------------------------------
Vertex present = previous.next; // ํ์ฌ ๊ฐ์ 6.next = 23์ ์ ์ฅ
previous.next = newNode; // 6์ next๋ฅผ ์๋ก์ด ๋
ธ๋(90) ์ผ๋ก ์ค์
newNode.next = present; // ์๋ก์ด ๋
ธ๋(90)์ next๋ฅผ 23์ผ๋ก ์ค์
๋ ธ๋๋ฅผ ๋ง์ง๋ง์ ์ถ๊ฐ
๋ง์ง๋ง์ 80์ด๋ผ๋ ๋ ธ๋๋ฅผ ์ถ๊ฐํ๋ค๊ณ ๊ฐ์ ํด๋ณด์.
๋ฐ๋ก tail๋ก ์ ๊ทผํด์ tail.next๋ฅผ 80์ผ๋ก ์ง์ ํด์ฃผ๋ฉด ๋์ด๋ค!
๋ฐ๋์ tail์ ๋ณ๊ฒฝํด์ฃผ์ด์ผ ํ๋ค!!
Vertex newNode = new Vertex(80); // 80์ ์๋ก ์์ฑํ๊ณ
tail.next = newNode; // tail.next๋ฅผ 80์ผ๋ก ์ง์ ํ ๋ค
tail = newNode; // ๋ค์ tail์ 80์ผ๋ก ๋ณ๊ฒฝํด์ค๋ค.
๋ฐ์ดํฐ๋ก ๋ ธ๋์ ์ธ๋ฑ์ค ์กฐํ
์ฐ๋ฆฌ๊ฐ 6์ ์ฐพ๊ณ ์ ํ๋ค๋ฉด head๋ถํฐ ๊ฒ์ฌ๋ฅผ ํ๋ฉด์ 6์ธ์ง ํ์ธ ํ 6์ด ๋ง๋ค๋ฉด ์ฆ๊ฐ ์์ผฐ๋ index๋ฅผ ๋ฐํํ๊ณ , ๋ง์ฝ cur.next๊ฐ null์ด๋ฉด NOT FOUND๋ฅผ ๋ฆฌํดํ๋ค.
// findNum = 6;
int index = 0;
Vertex cur = head;
while(cur != findNum) { // cur์ด 6์ธ๊ฐ?
cur = cur.next; // ์๋๋ฉด cur.next
index++; // ์ธ๋ฑ์ค ์ฆ๊ฐ
if(cur == null){
System.out.println("NOT FOUND");
return ;
}
}
System.out.println(findNum + "์ ์ธ๋ฑ์ค๋ : " + index); // findNum์ ์ธ๋ฑ์ค๋ : 3
return ;
์ธ๋ฑ์ค๋ก ๋ฐ์ดํฐ ์กฐํ
๋ง์ฝ์ ์ธ๋ฑ์ค๋ก 3์ ๋ฐ์๋ค๊ณ ํ๋ฉด, ํด๋นํ๋ ์๊ฐ 6์ผ ๊ฒ์ด๋ค.
๋ฉ์๋ ๋ด๋ถ์ count ๋ณ์๋ฅผ ๋ง๋ค์ด์ ํ๋์ฉ ์ฆ๊ฐ์ํค๋ฉด์ index์ ๊ฐ์์ง๋ ์์ ์ ํด๋น ๊ฐ์ ๋ฐํํ๋ฉด ๋๋ค.
// index = 3;(4๋ฒ์งธ ์)
int count = 0;
Vertex find = head;
while(count++ != index) { // count๋ฅผ ์ฆ๊ฐ์ํค๋ฉด์ index์ ๊ฐ์์ง๋ ์์ ์ ์ข
๋ฃํ๋ค.
find = find.next; // ๊ณ์ ๋ค์ ๋
ธ๋์ ๋ฐ์ดํฐ๋ก ๋ฐ๊ฟ์ค๋ค.
if(find == null) {
System.out.println("NOT FOUND");
return ;
}
}
System.out.println(index + "์ ํด๋นํ๋ ์๋ : " + find); // ์ด ์์ ์ count๋ 4(++๋ก ์ธํด)๊ฐ ๋์ด์์ด์ index๋ฅผ ์ถ๋ ฅ์ํจ๋ค.
return ;
๋ฐ์ดํฐ ์ญ์
์ธ๋ฑ์ค๊ฐ 3์ธ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๋ค๊ณ ๊ฐ์ ํด๋ณด์.
while๋ฌธ์ผ๋ก ์ผ๋จ ์ธ๋ฑ์ค๊ฐ 2์ธ ๋ ธ๋๋ฅผ ์ฐพ๊ณ , next๋ฅผ ์ญ์ ํ ๋ ธ๋์ ๋ค์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค!
// index : 3
Vertex pre = head;
while(--index != 0) {
pre = pre.next;
}
Vertex delNode = pre.next; // ์ญ์ ํ ๋
ธ๋ : 6
// next๋ฅผ ๋ฐ๊ฟ์ฃผ๊ธฐ
pre.next = delNode.next;
Delete delNode; // ํด๋น ๋
ธ๋ ์ญ์
References
VisuAlgo - Linked List (Single, Doubly), Stack, Queue, Deque
VisuAlgo is free of charge for Computer Science community on earth. If you like VisuAlgo, the only payment that we ask of you is for you to tell the existence of VisuAlgo to other Computer Science students/instructors that you know =) via Facebook, Twitter
visualgo.net
opentutorials.org/module/1335/8821
Linked list - Data Structure (์๋ฃ๊ตฌ์กฐ)
์๊ฐ Linked List๋ Array List์๋ ๋ค๋ฅด๊ฒ ์๋ฆฌ๋จผํธ์ ์๋ฆฌ๋จผํธ ๊ฐ์ ์ฐ๊ฒฐ(link)์ ์ด์ฉํด์ ๋ฆฌ์คํธ๋ฅผ ๊ตฌํํ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ๊ทธ๋์ ์ด๋ฆ๋ linked list์ ๋๋ค. ๊ทธ๋ ๊ฒ ๋ณด๋ฉด linked list์์ ๊ฐ์ฅ ์ค์ํ
opentutorials.org