๐Ÿ”ฅ TIL - Day 33

Kim Dae Hyunยท2021๋…„ 10์›” 20์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
38/93

๐Ÿ“Œ LinkedList ?

๋…ธ๋“œ์™€ ํฌ์ธํ„ฐ๋กœ ์ด๋ค„์ง„ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ Array์™€ ๊ฐ™์ด ์ผ๋ จ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.

Array๋ž‘ ๋ฌด์—‡์ด ๋‹ค๋ฅธ๊ฐ€?

  • Array์— ๋น„ํ•ด ์‚ฝ์ž…, ์‚ญ์ œ ๋“ฑ ์ˆ˜์ •์— ์šฉ์ดํ•˜๋‹ค.
    LinkedList์—์„œ ์ˆ˜์ •์€ ๋‹จ์ง€ ์—ฐ๊ฒฐ์„ ๋Š๊ณ  ๋˜ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
    Array๋Š” ์‚ฝ์ž…, ์‚ญ์ œ์‹œ ์ตœ์•…์˜ ๊ฒฝ์šฐ Array์˜ ํฌ๊ธฐ๋งŒํผ์˜ ์—ฐ์‚ฐํšŸ์ˆ˜๋ฅผ ๊ฐ–๋Š”๋‹ค (= O(N))

  • but, Array์— ๋น„ํ•ด ์กฐํšŒ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง„๋‹ค.
    ์กฐํšŒํ•˜๊ณ ์ž ํ•˜๋Š” ๋…ธ๋“œ๊ฐ€ ๊ฐ€์žฅ ๋์— ์žˆ๋‹ค๋ฉด O(N)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค. ํฌ์ธํ„ฐ๋ฅผ ํƒ€๊ณ  ์˜ฌ๋ผ๊ฐ€๋ฉฐ ์ญˆ~์šฑ ์ˆœํšŒํ•ด์•ผ ํ•จ!

  • ์กฐํšŒ๊ฐ€ ์ž์ฃผ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด ? Array ๐Ÿ‘

  • ์ˆ˜์ •์ด ์ž์ฃผ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด ? LinkedList ๐Ÿ‘


๐Ÿ“Œ LinkedList ์ง์ ‘ ๊ตฌํ˜„

LinkedList๋Š” ๊ฐ’์„ ๋‹ด๋Š” node์™€ ๋‹ค์Œ node๋ฅผ ๊ฐ€๋ฆฌํ‚ฌ pointer๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฐ ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค.

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

์ด์ œ Node ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด์„œ LinkedList๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
List์˜ ์ฃผ์š” ์—ฐ์‚ฐ์ธ ์กฐํšŒ, ์‚ฝ์ž…, ์‚ญ์ œ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

LinkedList๋Š” ๋ฐ˜๋“œ์‹œ ๋ฐ˜๋“œ์‹œ ๋ฐ˜๋“œ์‹œ Head ๋…ธ๋“œ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ LinkedList์˜ ์ƒ์„ฑ์ž์—์„œ ๋ฌด์กฐ๊ฑด Head ๋…ธ๋“œ๊ฐ€ ์ƒ์„ฑ๋˜๋„๋ก ํ•œ๋‹ค.

class LinkedList:
    def __init__(self, data):
        self.head = Node(data)

...

# ์ƒ์„ฑ
linked_list = LinkedList(1)

LinkedList๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๊ฒƒ์— ์žˆ์–ด์„œ ๋ฝ€์ธํŠธ๋Š” LinkedList ์ˆœํšŒ์‹œ head ๋…ธ๋“œ๋ฅผ ์ง์ ‘ ์›€์ง์ด์ง€ ์•Š๊ณ  ๋ณต์‚ฌํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. (head๋Š” ์›€์ง์ด๋ฉด ์•ˆ๋จ!)

cur = self.head

์กฐํšŒ (n๋ฒˆ์งธ ๋…ธ๋“œ ์กฐํšŒ)

index ๊นŒ์ง€ next๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉฐ ํฌ์ธํ„ฐ๋ฅผ ์˜ฎ๊ธฐ๊ณ  index๋ฒˆ ์งธ์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ ๋ฃจํ”„๋ฅผ ๋‚˜์™€์„œ ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

def get(self, index):
    cur = self.head
    cnt = 0
    while cnt < index:
        cur = cur.next
        cnt += 1
    return cur

์‚ฝ์ž… (๊ฐ€์žฅ ๋’ค์— ์‚ฝ์ž… append)

head ๋…ธ๋“œ๋ถ€ํ„ฐ next๋ฅผ ์ด์šฉํ•ด์„œ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๋ฉด์„œ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ ์ด๋™ํ•œ๋‹ค (next)

๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ None์ด๋ผ๋ฉด While ๋ฌธ์„ ํƒˆ์ถœํ•˜๊ณ  ํ•ด๋‹น node์˜ next๋กœ ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

def append(self, data):
    if self.head is None: # ํ—ค๋“œ๋…ธ๋“œ ์—†๋Š” ๊ฒฝ์šฐ ์ถ”๊ฐ€๋˜๋Š” ๋…ธ๋“œ๊ฐ€ ํ—ค๋“œ๋…ธ๋“œ๊ฐ€ ๋˜๋„๋ก ์ฒ˜๋ฆฌ
        self.head = Node(data)
        return

    cur = self.head
    while cur.next is not None:
        cur = cur.next
    cur.next = Node(data)

์‚ฝ์ž… (์ค‘๊ฐ„์— ์‚ฝ์ž… insert)

n๋ฒˆ์งธ์— ์‚ฝ์ž…ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด n-1๋ฒˆ์งธ ๋…ธ๋“œ์˜ next๊ฐ€ ์‚ฝ์ž…ํ•˜๊ณ ์ž ํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์›๋ž˜ ์‚ฝ์ž…๋˜๋Š” ๋…ธ๋“œ๋Š” ์›๋ž˜ n๋ฒˆ์งธ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ•˜๋ฉด ๋œ๋‹ค.

์ค‘์š”ํ•œ ๊ฒƒ์€ ์›๋ž˜์˜ n-1 ๋ฒˆ์งธ ๋…ธ๋“œ์™€ n๋ฒˆ์งธ ๋…ธ๋“œ์˜ ์—ฐ๊ฒฐ์„ ๋Š๊ธฐ ์ด์ „์— n๋ฒˆ์งธ ๋…ธ๋“œ๋ฅผ ๋ฐ˜๋“œ์‹œ ์ €์žฅํ•ด๋‘ฌ์•ผ ํ•œ๋‹ค. ์•„๋‹ˆ๋ฉด ๋‹ค์‹œ ์ฐธ์กฐํ•  ์ˆ˜ ์—†๋‹ค.

[ ์ด๋ถ€๋ถ„ !! ]
next_node = curr_node.next
def insert(self, index, value):
    if index == 0:
        next_node = self.head
        self.head = Node(value)
        self.head.next = next_node
        return
    # -1 ๋ฒˆ์งธ๋ฅผ ์ฐพ๋Š”๋‹ค๋ฉด ์–ธ์ œ๋‚˜ 0์ผ ๋•Œ ์˜ˆ์™ธ์ƒํ™ฉ์„ ์ƒ๊ฐนํ•ด์•ผ ํ•œ๋‹ค.
    curr_node = self.get(index - 1)
    new_node = Node(value)
    next_node = curr_node.next
    curr_node.next = new_node
    new_node.next = next_node

์‚ญ์ œ

์ œ์ผ ๊ฐ„๋‹จํ•˜๋‹ค ๐Ÿ‘

def delete(self, index):
    if index == 0:
        self.head = self.head.next
        return
    prev_node = self.get(index - 1)
    prev_node.next = prev_node.next.next

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฃผ๊ฐ„ Start !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

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