๊ด€๋ฆฌ ๋ฉ”๋‰ด

DOing

[์ž๋ฃŒ๊ตฌ์กฐ] ๋ฆฌ์ŠคํŠธ with Python ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[์ž๋ฃŒ๊ตฌ์กฐ] ๋ฆฌ์ŠคํŠธ with Python

mangdo 2021. 5. 18. 12:17

 

๐Ÿ’ก Linked List(์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ)

๋ฐฐ์—ด(array)์€ ๋ฏธ๋ฆฌ ๊ณต๊ฐ„์„ ์˜ˆ์•ฝํ•ด๋†“๊ณ  ๊ทธ ๊ณต๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์“ฐ๊ณ  ์ฝ๋Š”๋‹ค.

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ(linked list)๋Š” ๋ฏธ๋ฆฌ ์˜ˆ์•ฝ์„ ํ•ด๋†“์ง€์•Š๊ณ  ํ•„์š”ํ• ๋•Œ๋งˆ๋‹ค ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋‹ค.

๋ฐฐ์—ด์€ ๋ฐ์ดํ„ฐ๋งŒ ์ €์žฅํ•˜์ง€๋งŒ, linked list๋Š” (๋ฐ์ดํ„ฐ+๋‹ค์Œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๋ฅดํ‚ค๋Š” ์ฃผ์†Œ)๋ฅผ ์ €์žฅํ•œ๋‹ค.

 

- ๋…ธ๋“œ : ๋ฐ์ดํ„ฐ ์ €์žฅ๋‹จ์œ„(๋ฐ์ดํ„ฐ๊ฐ’, ํฌ์ธํ„ฐ)๋กœ ๊ตฌ์„ฑ

- ํฌ์ธํ„ฐ : ๊ฐ ๋…ธ๋“œ์•ˆ์—์„œ ๋‹ค์Œ์ด๋‚˜ ์ด์ „๋ฐ์ดํ„ฐ์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ณต๊ฐ„

 

๐Ÿ’ก ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ ์žฅ๋‹จ์ 

์žฅ์ :

1) ๋ฏธ๋ฆฌ ๊ณต๊ฐ„์„ ํ• ๋‹นํ•˜์ง€์•Š์•„๋„๋œ๋‹ค.

2) ์—ฐ์†๋œ ๊ณต๊ฐ„์„ ํ• ๋‹นํ•  ํ•„์š”๋„ ์—†๋‹ค.

 

๋‹จ์ :

1) ์ €์žฅ๊ณต๊ฐ„์˜ ํšจ์œจ์ด ๋†’์ง€์•Š๋‹ค.

   -> ๋ฐ์ดํ„ฐ ๋ฟ๋งŒ์•„๋‹ˆ๋ผ ์ฃผ์†Œ๋ฅผ ์œ„ํ•œ ๊ณต๊ฐ„์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

2) ์ ‘๊ทผ ์†๋„๊ฐ€ ๋Š๋ฆฌ๋‹ค.

   -> ๋ฐฐ์—ด์€ ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•ด ๋น ๋ฅด๊ฒŒ ์ ‘๊ทผํ•˜์ง€๋งŒ, Linked list๋Š” ์—ฐ๊ฒฐ์ •๋ณด๋ฅผ ์ฐพ๋Š” ์‹œ๊ฐ„์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

3) ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ ์‚ญ์ œ์‹œ ์•ž ๋’ค ๋ฐ์ดํ„ฐ์˜ ์—ฐ๊ฒฐ์„ ์žฌ๊ตฌ์„ฑํ•˜๋Š” ๋ถ€๊ฐ€์ ์ธ ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.

 

๐Ÿ’ก Array VS List

   ์ถ”๊ฐ€ / ์‚ญ์ œ  ์กฐํšŒ
Array ๋Š๋ฆผ ๋น ๋ฆ„
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, end=' ')
            node=node.next
      
    def delete(self, data):
        # ์•„๋ฌด๋Ÿฐ ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค.
        if self.head =='':
            print("๋…ธ๋“œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
            return
        # head ๋ฐ์ดํ„ฐ ์‚ญ์ œ
        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 #๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜€๋‹ค๋ฉด null
                    del temp
                else:
                    node=node.next
            
        
#์‹คํ–‰
linkedlist1 = NodeMgmt(0)
linkedlist1.desc() #0
print()
for data in range(1,10):
	linkedlist1.add(data)
linkedlist1.desc() #0~9๊นŒ์ง€ ์ถœ๋ ฅ
print()
linkedlist1.delete(4)
linkedlist1.desc()#0 1 2 3 5 6 7 8 9

๐Ÿ’ก Double Linked List

๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ํ›„๋ฐ˜์— ์žˆ๋‹ค๋ฉด ๋ฌด์กฐ๊ฑด ์•ž์—์„œ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰์„ ํ•˜๋Š” Linked list๋Š” ์˜ค๋ž˜๊ฑธ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

์ด๋ฅผ ๋ณด์™„ํ•œ double linked list๋Š” ๊ตฌ์กฐ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

๐Ÿ’ก ๋”๋ธ” ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ ๊ตฌํ˜„ ์ฝ”๋“œ

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 #์ฒ˜์Œ์—๋Š” ๋…ธ๋“œ๊ฐ€ ํ•˜๋‚˜๋ผ์„œ head=tail
        
    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, end=' ')
            node=node.next
     
    # head๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ํ•˜๋Š” ์ฝ”๋“œ
    def search_from_head(self, data):
        if self.head == None: #๋ฐฉ์–ด์ฝ”๋“œ
            return False
        node = self.head
        while node:
            if node.data == data:
                return node
            else:
            	node = node.next
        return False
            
    # tail๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ํ•˜๋Š” ์ฝ”๋“œ
    def search_from_tail(self, data):
     	
        if self.head == None:
        	return False
            
        node = self.tail
        while node:
            if node.data == data:
                return node
            else:
            	node = node.prev
        return False
     
    # ํŠน์ • ์ˆซ์ž ์•ž์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜
    def insert_before(self, data, before_data):
        if self.head == None: # ์•„์˜ˆ ์—†๋‹ค๋ฉด
            return False
        else:
            node = self.tail
            while node.data != before_data:
                node = node.prev
                if node == None:
                	return False
            new = Node(data)
            before_new = node.prev
            new.next = node
            new.prev = before_new
            
            node.prev = new
            before_new.next = new
            return True
        
  
# 0~9๊นŒ์ง€ ๋„ฃ๊ธฐ
double_linked_list = NodeMgmt(0)
for data in range(1,10):
  	double_linked_list.insert(data)
double_linked_list.desc()
print()

# ๋ฐ์ดํ„ฐ 2์•ž์— 1.5๋„ฃ๊ธฐ
double_linked_list.insert_before(1.5, 2)
double_linked_list.desc()
print()

# ๊ฒ€์ƒ‰
search = double_linked_list.search_from_tail(1.5)
print(search.data)