Data structures (with python) - 4. Linked List

2024. 1. 6. 21:52ใ†CS - Roadmap.sh/1. Data structures

Data structures (with python) - 4. Linked List

 

๐Ÿ’ก Arrays store elements in contiguous memory locations, resulting in easily calculable addresses for the elements stored and this allows faster access to an element at a specific index. Linked lists are less rigid in their storage structure and elements are usually not stored in contiguous locations, hence they need to be stored with additional tags giving a reference to the next element. This difference in the data storage scheme decides which data structure would be more suitable for a given situation.

๋ฐฐ์—ด์€ ์š”์†Œ๋ฅผ ์ธ์ ‘ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์œ„์น˜์— ์ €์žฅํ•˜๋ฏ€๋กœ ์ €์žฅ๋œ ์š”์†Œ์˜ ์ฃผ์†Œ๋ฅผ ์‰ฝ๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์–ด ํŠน์ • ์ธ๋ฑ์Šค์˜ ์š”์†Œ์— ๋” ๋น ๋ฅด๊ฒŒ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ๋œ ๋ชฉ๋ก์€ ์ €์žฅ ๊ตฌ์กฐ๊ฐ€ ๋œ ์—„๊ฒฉํ•˜๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ์š”์†Œ๊ฐ€ ์ธ์ ‘ํ•œ ์œ„์น˜์— ์ €์žฅ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋‹ค์Œ ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š” ์ถ”๊ฐ€ ํƒœ๊ทธ์™€ ํ•จ๊ป˜ ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐ์ดํ„ฐ ์ €์žฅ ์ฒด๊ณ„์˜ ์ฐจ์ด์— ๋”ฐ๋ผ ์ฃผ์–ด์ง„ ์ƒํ™ฉ์— ๋” ์ ํ•ฉํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๊ฐ€ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค.

 

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(Linked List)๋Š” ๋ฐ์ดํ„ฐ ์š”์†Œ๋“ค์ด ๋ฉ”๋ชจ๋ฆฌ์˜ ์—ฐ์†์ ์ธ ์œ„์น˜์— ์ €์žฅ๋˜์ง€ ์•Š๊ณ , 

๊ฐ ์š”์†Œ(node)๊ฐ€ ๋‹ค์Œ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ž๋ฃŒ๊ตฌ์กฐ ์ž…๋‹ˆ๋‹ค.

Linked list์˜ ๊ฐ ๋…ธ๋“œ๋Š” ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 

1. ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„

2. ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ถ€๋ถ„(๋งํฌ ๋˜๋Š” ํฌ์ธํ„ฐ) ์ž…๋‹ˆ๋‹ค.

 

Linked list ์˜ ํŠน์ง•

- ๋…ธ๋“œ๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ์–ด๋Š ๊ณณ์ด๋“  ๋ฐฐ์น˜๋  ์ˆ˜ ์žˆ๋‹ค.

์ด๋Š” array ์™€ ๋Œ€์กฐ์ ์ธ ํŠน์ง•์œผ๋กœ, array์˜ ์š”์†Œ๋“ค์€ ์—ฐ์†์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ์œ„์น˜์— ๋ฐฐ์—ด๋œ๋‹ค.

 

- ์ฝ”๋“œ์˜ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

์ด๋Š” ์‚ฝ์ž… ๋˜๋Š” ์‚ญ์ œํ•  ๋…ธ๋“œ์˜ ๋งํฌ๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ๋˜๊ธฐ ๋–„๋ฌธ์ด๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ์‚ฝ์ž… ๋˜๋Š” ์‚ญ์ œํ•  ๋…ธ๋“œ์˜ ์ด์ „ ๋…ธ๋“œ๋ฅผ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

- ๋ฐฐ์—ด๊ณผ๋Š” ๋‹ฌ๋ฆฌ linked list์˜ ํฌ๊ธฐ๋Š” ๋™์ ์œผ๋กœ ๋ณ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

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

๋…ธ๋“œ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        if not self.head:
            self.head = Node(data)
        else:
            current = self.head
            while current.next:
                current = current.next
            current.next = Node(data)

    def display(self):
        elements = []
        current_node = self.head
        while current_node:
            elements.append(current_node.data)
            current_node = current_node.next
        print(elements)

๊ทธ ํ›„, Linked list๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

linked_list = LinkedList()
linked_list.append('A')
linked_list.append('B')
linked_list.append('C')
linked_list.display()  # ์ถœ๋ ฅ: ['A', 'B', 'C']

์ด์™€ ๊ฐ™์ด ํŒŒ์ด์ฌ์—์„œ Linked list๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ™œ์šฉ

์ฃผ๋กœ '๋ณ‘ํ•ฉ ์ •๋ ฌ(merge sort)' ์— ์‚ฌ์šฉ๋œ๋‹ค.

๋ณ‘ํ•ฉ ์ •๋ ฌ์€ ๋ถ„ํ• ์ •๋ณต(Divide and Conquer) ์ „๋žต์„ ์ด์šฉํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ,

์›์†Œ๋“ค์„ ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„๋กœ ๋ถ„ํ• ํ•œ ํ›„ ๋ณ‘ํ•ฉํ•˜๋ฉด์„œ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

์ด ๋–„, Linked list๋Š” ๋ถ„ํ• ๊ณผ ๋ณ‘ํ•ฉ ๊ณผ์ •์—์„œ ์š”์†Œ์˜ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ๊ฐ€ ํšจ์œจ์ ์ด๋ผ๋Š” ์ด์ ์„ ์‚ด๋ ค ์ด ๊ณผ์ •์„ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

1. ๋ถ„ํ•  ๋‹จ๊ณ„์—์„œ๋Š” Linked list๋ฅผ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„๋Š” ์ž‘์—…์ด ์ด๋ฃจ์–ด์ง„๋‹ค.

์ด ์ž‘์—…์€ 

๋น ๋ฅธ ํฌ์ธํ„ฐ( ํ•œ ๋ฒˆ์— ๋‘์นธ ์”ฉ ์ด๋™) a.k.a ํ† ๋ผ

๋Š๋ฆฐ ํฌ์ธํ„ฐ( ํ•œ๋ฒˆ์— ํ•œ ์นธ์”ฉ ์ด๋™) a.k.a ๊ฑฐ๋ถ์ด

์„ ์‚ฌ์šฉํ•ด์„œ ๋ฆฌ์ŠคํŠธ์˜ ์ค‘๊ฐ„์„ ์ฐพ๊ณ , ๋ฆฌ์ŠคํŠธ์˜ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค

 

2. ๋ณ‘ํ•ฉ ๋‹จ๊ณ„์—์„œ ๋‘ ๊ฐœ์˜ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์˜ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ‘ํ•ฉํ•œ๋‹ค.

์ด ๊ณผ์ •์—์„œ ๋‘ ๋ฆฌ์ŠคํŠธ์˜ ๋จธ๋ฆฌ๋ถ€ํ„ฐ ๋น„๊ตํ•˜๋ฉด์„œ ์ž‘์€ ๊ฐ’์„ ์„ ํƒํ•ด, ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•˜๊ณ ,

์„ ํƒ๋œ ๋…ธ๋“œ๋ฅผ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™์‹œํ‚จ๋‹ค.

 

์ด ์™ธ์—๋Š”

์ฒด์ด๋‹ (ํ•ด์‹œ ํ…Œ์ด๋ธ”์˜ ์ถฉ๋Œ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์ค‘ ํ•˜๋‚˜),

LRU(Least Recnetly Used) ์บ์‹œ ๋“ฑ ์— ํ™œ์šฉ๋œ๋‹ค.

 

์ฝ”ํ…Œ ๊ณต๋žต

 

1. ๋‘ ์ˆ˜์˜ ๋ง์…ˆ

๋‘ ๊ฐœ์˜ Linked list ๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ,

๊ฐ list์˜ ๋…ธ๋“œ๋Š” ์ˆซ์ž์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•œ๋‹ค. ๋‘ linked list๋ฅผ ๋”ํ•˜์—ฌ ์ƒˆ๋กœ์šด Linked list๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๊ฐ ์ž๋ฆฌ์ˆ˜์˜ ์—ฐ์‚ฐ๊ณผ ์˜ฌ๋ฆผ์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ linked list์˜ ์ˆœํšŒ ๋ฐ ๋…ธ๋“œ ๊ด€๋ฆฌ ๋Šฅ๋ ฅ์„ ํ…Œ์ŠคํŠธ.

 

2. ์ˆœํ™˜ Linked list ํŒ๋ณ„

์ฃผ์–ด์ง„ Linked list๊ฐ€ ์ˆœํ™˜ํ•˜๋Š”์ง€ ( = ๋…ธ๋“œ๊ฐ€ ์ž๊ธฐ ์ž์‹ ์„ ๋‹ค์‹œ ๊ฐ€๋ฆฌํ‚ค๋Š”์ง€) ํŒ๋ณ„ํ•˜๋Š” ๋ฌธ์ œ

์ˆœํ™˜์„ ํŒ๋ณ„ํ•˜๊ธฐ์œ„ํ•ด Floyd's cycle detection algorithm์„ ์‚ฌ์šฉํ•œ๋‹ค.

์ฐธ๊ณ  : https://snutiise.github.io/Floyd's-Cycle-Detection-Algorithm/

 

ํ”Œ๋กœ์ด๋“œ ์ˆœํ™˜ ์ฐพ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ Floyd's Cycle Detection Algorithm

 

snutiise.github.io

 

"ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด" ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด ์ˆœํ™˜ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ํŒ๋ณ„์„ ํŒŒ์ด์ฌ์—์„œ ํ•ด๋ณด์ž.

 

# linked list ๊ตฌํ˜„
class Node:
    def __init__(self, x):
        self.val = x
        self.next = None

 

def has_cycle(head):
    if head is None:
        return False

    slow = head  # ๊ฑฐ๋ถ์ด
    fast = head  # ํ† ๋ผ

    while fast is not None and fast.next is not None:
        slow = slow.next
        fast = fast.next.next

        if slow == fast:  # ๊ฑฐ๋ถ์ด์™€ ํ† ๋ผ๊ฐ€ ๋งŒ๋‚˜๋ฉด ์ˆœํ™˜ ์žˆ๋‹ค๊ณ  ํŒ๋‹จ
            return True

    return False

์œ„ ํ•จ์ˆ˜๋Š” ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด๊ฐ€ ๊ฐ™์€ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ฌ ๋•Œ ์ˆœํ™˜์„ ๊ฐ์ง€ํ•œ๋‹ค.

ํ† ๋ผ๋Š” ๋‘์นธ์”ฉ, ๊ฑฐ๋ถ์ด๋Š” ํ•œ ์นธ ์”ฉ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์ˆœํ™˜์ด ์กด์žฌํ•œ๋‹ค๋ฉด ํ† ๋ผ๋Š” ๊ฒฐ๊ตญ ๊ฑฐ๋ถ์ด๋ฅผ ๋”ฐ๋ผ ์žก๋Š”๋‹ค. ๋Š” ๊ฐ€์ •์„ ๋ณธ๋‹ค.

 

# Linked list ์ƒ์„ฑ
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = head.next  # ์ˆœํ™˜ ์ƒ์„ฑ

# ์ˆœํ™˜ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธ
print(has_cycle(head))  # True๊ฐ€ ์ถœ๋ ฅ๋˜์–ด์•ผ ํ•จ

์œ„ ์ฝ”๋“œ์—์„œ๋Š” ์ˆœํ™˜ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ˆœํ™˜ ์—ฌ๋ถ€ ํ…Œ์ŠคํŠธํ•œ๋‹ค.

๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๊ฐ€ 2๋ฒˆ์จฐ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ์„ค์ •ํ•ด ์ˆœํ™˜์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ has_cycle์€ True๊ฐ€ ๋‚˜์˜จ๋‹ค.

728x90