์•Œ๊ณ ๋ฆฌ์ฆ˜

[Data Structure] ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ (Linked List)

jjingle 2024. 1. 26. 14:19

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ (Linked List) ?

  • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ
  • ๋ฐฐ์—ด์€ ์ˆœ์ฐจ์ ์œผ๋กœ ์—ฐ๊ฒฐ๋œ ๊ณต๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜์—ดํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ
  • ๋–จ์–ด์ง„ ๊ณณ์— ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ™”์‚ดํ‘œ๋กœ ์—ฐ๊ฒฐํ•ด์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ
  • C์—์„œ๋Š” ์ฃผ์š”ํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ง€๋งŒ, Python์€ ๋ฆฌ์ŠคํŠธ ํƒ€์ž…์ด ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ์ง€์›
  • ๊ธฐ๋ณธ ๊ตฌ์กฐ
    • ๋…ธ๋“œ(Node) : ๋ฐ์ดํ„ฐ ์ €์žฅ ๋‹จ์œ„(๋ฐ์ดํ„ฐ๊ฐ’, ํฌ์ธํ„ฐ)๋กœ ๊ตฌ์„ฑ
    • ํฌ์ธํ„ฐ(Pointer) : ๊ฐ ๋…ธ๋“œ ์•ˆ์—์„œ, ๋‹ค์Œ์ด๋‚˜ ์ด์ „์˜ ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ณต๊ฐ„
  • ์žฅ์  (C์–ธ์–ด ์ž…์žฅ์—์„œ)
    • ๋ฏธ๋ฆฌ ๋ฐ์ดํ„ฐ ๊ณต๊ฐ„์„ ๋ฏธ๋ฆฌ ํ• ๋‹นํ•˜์ง€ ์•Š์•„๋„ ๋จ (๋ฐฐ์—ด์€ ํ• ๋‹น ํ•„์š”)
  • ๋‹จ์  (C์–ธ์–ด ์ž…์žฅ์—์„œ)
    • ์—ฐ๊ฒฐ์„ ์œ„ํ•œ ๋ณ„๋„ ๋ฐ์ดํ„ฐ ๊ณต๊ฐ„์ด ํ•„์š”ํ•˜๋ฏ€๋กœ, ์ €์žฅ๊ณต๊ฐ„ ํšจ์œจ์ด ๋†’์ง€ ์•Š์Œ
    • ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ์ฐพ๋Š” ์‹œ๊ฐ„์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ์ ‘๊ทผ ์†๋„๊ฐ€ ๋Š๋ฆผ
    • ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ ์‚ญ์ œ ์‹œ, ๋ฐ์ดํ„ฐ์˜ ์—ฐ๊ฒฐ์„ ์žฌ๊ตฌ์„ฑํ•ด์•ผํ•˜๋Š” ๋ถ€๊ฐ€์ ์ธ ์ž‘์—… ํ•„์š”

 

node : ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ
data pointer
: ๋‹ค์Œ ๋ฐ์ดํ„ฐ ์ฃผ์†Œ

 

class Node:
	def __init__(self, data):
    	self.data = data
        self.next = None
        
        
# ์œ„ ์ฝ”๋“œ ๋‹ค๋ฅด๊ฒŒ ํ‘œํ˜„
class Node:
	def __init__(self, data, next=None):
    	self.data = data
        self.next = next
        
# Node์™€ Node ์—ฐ๊ฒฐ (ํฌ์ธํ„ฐ ํ™œ์šฉ)
node1 = Node(1)
node2 = Node(2)
node1.next = node(2)
head = node1

# ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
def add(data):
	node = head
    while node.next:         #๋งˆ์ง€๋ง‰ ์œ„์น˜ ์ฐพ๋Š” ๊ณผ์ •
    	node = node.next
    node.next = Node(data)   #node.next = None ์ธ ๊ณณ์—, data ์ถ”๊ฐ€

 

 

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์‚ฌ์ด์—, ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€

# node : 1, 2, 3, 4, 5, 6, 7, 8, 9

node3 = Node(1.5)

node = head
search = True
while search:
	if node.data == 1:
    	search = False
    else:
    	node = node.next

node_next = node.next
node.next = node3
node3.next = node_next

# => new node : 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9

 

 

Python ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์œผ๋กœ Linked List ๊ตฌํ˜„

class Node:
    def __init__(self, data, next=None):
        self.data = data
        self.next = next
    
class NodeMgmt:
    def __init__(self, data):
        self.head = Node(data)
        
    def add(self, data):
        if self.head == '':
            self.head = Node(data)
        else:
            node = self.head
            while node.next:
                node = node.next
            node.next = Node(data)
        
    def desc(self):
        node = self.head
        while node:
            print (node.data)
            node = node.next
            
    def delete(self, data):
        if self.head == '':
            print ("ํ•ด๋‹น ๊ฐ’์„ ๊ฐ€์ง„ ๋…ธ๋“œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
            return
        
        if self.head.data == data:
            temp = self.head
            self.head = self.head.next
            del temp
        else:
            node = self.head
            while node.next:
                if node.next.data == data:
                    temp = node.next
                    node.next = node.next.next
                    del temp
                    return
                else:
                    node = node.next
    
    
    
    linkedlist1 = NodeMgmt(0)
    linkedlist1.desc()         #0
    linkedlist1.head           #<__main__.Node at 0x1099fc6a0>
    linkedlist1.delete(0)      # -> ์ถœ๋ ฅ๊ฒฐ๊ณผ๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์€ head๊ฐ€ ์‚ญ์ œ๋จ์„ ์˜๋ฏธ
    
    linkedlist1 = NodeMgmt(0)
    linkedlist1 = NodeMgmt(1)
    linkedlist1.delete(0)  
    linkedlist1.desc()         #1

 

 

๋”๋ธ” ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ

  • ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ
  • ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์„œ, ๋…ธ๋“œ ํƒ์ƒ‰์ด ์–‘์ชฝ์œผ๋กœ ๋ชจ๋‘ ๊ฐ€๋Šฅ -> ํ•ญ์ƒ ์•ž์—์„œ ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ํ•ด์•ผํ•˜๋Š” ๊ธฐ์กด ๋‹จ์  ๊ทน๋ณต
class Node:
    def __init__(self, data, prev=None, next=None):
        self.prev = prev
        self.data = data
        self.next = next

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

    def insert(self, data):
        if self.head == None:
            self.head = Node(data)
            self.tail = self.head
        else:
            node = self.head
            while node.next:
                node = node.next
            new = Node(data)
            node.next = new
            new.prev = node
            self.tail = new

    def desc(self):
        node = self.head
        while node:
            print (node.data)
            node = node.next