Linked List

์ตœ์ œ์›ยท2023๋…„ 1์›” 23์ผ
0

์ž๋ฃŒ๊ตฌ์กฐ

๋ชฉ๋ก ๋ณด๊ธฐ
2/4
post-thumbnail

๐Ÿ’ก Linked List

Linked List๋Š” Node๋ผ๋Š” ๊ตฌ์กฐ์ฒด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”๋ฐ,
Node๋Š” ๋ฐ์ดํ„ฐ ๊ฐ’๊ณผ ๋‹ค์Œ Node์˜ address๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค
Linkde List๋Š” ๋ฌผ๋ฆฌ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ์ƒ์—์„œ๋Š” ๋น„์—ฐ์†์ ์ธ ์ €์žฅ์ด ๋˜์ง€๋งŒ
Linked List๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐ๊ฐ์˜ Node์˜ address๋ฅผ ๊ฐ€๋ฆฌํ‚ด์œผ๋กœ์จ
๋…ผ๋ฆฌ์ ์ธ ์—ฐ์†์„ฑ์„ ๊ฐ€์ง„ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค

Linked List๋Š” tree, graph๋“ฑ ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์ž์ฃผ ์“ฐ์ด๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค

Linked list๋ฅผ ์„ค๋ช…ํ•  ๋•Œ์—๋Š” ์ด๋Ÿฌํ•œ ์žฅ์ ๋“ค์„ ์„ค๋ช…ํ•˜๋ฉด ์ข‹๋‹ค

  1. ๋ฉ”๋ชจ๋ฆฌ์ƒ์—์„œ ๋ถˆ์—ฐ์†์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋œ๋‹ค๋Š” ์ 

  2. Node์˜ next address๋ฅผ ํ†ตํ•ด ๋ถˆ์—ฐ์†์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ
    ๋…ผ๋ฆฌ์  ์—ฐ์†์„ฑ์„ ๋ณด์žฅํ•œ๋‹ค๋Š” ์ 

  3. ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”๊ฐ€ ๋˜๋Š” ์‹œ์ ์—์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๊ธฐ ๋•Œ๋ฌธ์—
    ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ด ์ข‹๋‹ค๋Š” ์ 


Linked list ๊ตฌ์กฐ

lined list๋Š” Array์™€ ๋‹ฌ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น๋˜๋Š” ๊ฒฝ์šฐ์—

์—ฐ์†์„ฑ์„ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š๋‹ค ์ฆ‰ ๋’ค์ฃฝ๋ฐ•์ฃฝ์˜ ์ €์žฅ ์ˆœ์„œ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค

ํ•ด๋‹น ๊ทธ๋ฆผ์˜ data๋“ค๋„ linked list์˜ ํŠน์„ฑ์„ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—

๋ฉ”๋ชจ๋ฆฌ ์ƒ์˜ ์ €์žฅ ์ˆœ์„œ๋Š” ๋’ค์ฃฝ๋ฐ•์ค„์ผ ๊ฒƒ์ด๋‹ค

๊ทธ๋Ÿฌ๋‚˜ linked list์˜ ํŠน์ง•์ด ์—ฌ๊ธฐ์„œ ๋” ๋‚˜ํƒ€๋‚˜๋Š”๋ฐ

1๋ฒˆ์งธ Data์˜ ์ฃผ์†Œ๊ฐ’์€ 500๋ฒˆ์„ ๊ฐ–๊ณ  ์žˆ์ง€๋งŒ next address ๋Š” 1004๋ฒˆ์„ ํ–ฅํ•œ๋‹ค
(์ถ”๊ฐ€๋กœ 1๋ฒˆ์งธ Data๋Š” Head๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค)

์ฆ‰ ์ €์žฅ๋œ ๊ณต๊ฐ„์€ ์„œ๋กœ ๋‹ค๋ฅด์ง€๋งŒ 1๋ฒˆ์งธ Data์™€ 2๋ฒˆ์งธ Data๋Š” ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”๊ฒƒ ์ด๋‹ค

์—ฌ๊ธฐ์„œ ๋ด์•ผ ํ•  ์ ์€ ๋งˆ์ง€๋ง‰ Node์˜ next address๋Š” NULL ๊ฐ’์„ ์ง€๋‹Œ๋‹ค๋Š” ๊ฒƒ ์ด๋‹ค

์ด๊ฒƒ์ด ๋…ผ๋ฆฌ์  ์—ฐ์†์„ฑ์ด๋‹ค

๐Ÿ’ก ๋…ผ๋ฆฌ์  ์—ฐ์†์„ฑ

๊ฐ Node๋“ค์€ Next Address ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
๋…ผ๋ฆฌ์ ์œผ๋กœ ์—ฐ์†์„ฑ์„ ์œ ์ง€ํ•˜๋ฉด์„œ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค
Array์˜ ๊ฒฝ์šฐ ์—ฐ์†์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ฌผ๋ฆฌ์  ๋ฉ”๋ชจ๋ฆฌ ์ƒ์—์„œ
์ˆœ์ฐจ์ ์œผ๋กœ ์ €์žฅํ•˜ ๋Š” ๋ฐฉ๋ฒ•์„ ์ฑ„ํƒํ•˜๊ณ 
Linked list์—์„œ๋Š” ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์—ฐ์†์„ฑ์„ ์œ ์ง€ํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ธฐ ๋•Œ๋ฌธ์—
๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์ด ์ข€ ๋” ์ž์œ ๋กœ์šด ๋Œ€์‹ , Next Address๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์ €์žฅํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—
๋ฐ์ดํ„ฐ ํ•˜๋‚˜๋‹น ์ฐจ์ง€ํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋” ์ปค์ง€๊ฒŒ ๋œ๋‹ค


Linked list operation

์™ผ์ชฝ ์ •๋ ฌ๊ฐ€์šด๋ฐ ์ •๋ ฌ
accessO(n)
searchO(n)
insertionO(1)
deletionO(1)

๊ทธ๋ฆผ์—์„œ์™€ ๊ฐ™์ด B์™€ C์‚ฌ์ด์— E๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๊ฒฝ์šฐ์—

B์˜ Next Address โžก E์˜ Address

E์˜ Next Address โžก C์˜ Address

Array์™€ ๋‹ฌ๋ฆฌ Next Address๋งŒ ํ• ๋‹นํ•ด์ฃผ๋Š”๊ฑธ๋กœ ์ค‘๊ฐ„ ์‚ฝ์ž…์„ ์™„๋ฃŒํ•  ์ˆ˜ ์žˆ๋‹ค

delete๋Š” ๋ฐ˜๋Œ€๋กœ ํ• ๋‹นํ•˜๋ฉด ๋œ๋‹ค ๋งŒ์•ฝ E๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด

B์˜ Next Address โžก C์˜ Address ๋กœ ์„ค์ •ํ•˜๋ฉด ๋์ด๋‹ค

์„ค๋ช…์—์„œ์™€ ๊ฐ™์ด Linked list์˜ append delete์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(1)์ด๋‹ค

Array์˜ O(n)์˜ ์‹œ๊ฐ„๋ณด๋‹ค ๋น ๋ฅธ ์†๋„๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค

๋ฐ˜๋ฉด์— linked list์˜ access๋Š” O(n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ํ• ๋‹น๋œ๋‹ค

Array์—์„œ๋Š” random access๊ฐ€ ์ง€์›์ด ๋˜์ง€๋งŒ linked access๋Š” ์ˆœ์ฐจ์  ์ ‘๊ทผ๋ฐ–์— ํ—ˆ์šฉ์ด ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค

search ๋˜ํ•œ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€