YS's develop story

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

Data Structure

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

Yusang 2021. 7. 23. 09:49

๐Ÿ‘จ๐Ÿผ‍๐Ÿ’ป Linked List (๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ) ์ •๋ฆฌ With Python

๐Ÿฅ Linked List๋ž€? 

๐Ÿˆ Linked List ๊ตฌ์กฐ 

Linked List์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ์œ„ ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋…ธ๋“œ : ๋ฐ์ดํ„ฐ์˜ ์ €์žฅ ๋‹จ์œ„, ๋…ธ๋“œ๋Š” ๋ฐ์ดํ„ฐ์™€ ํฌ์ธํ„ฐ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

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

 

๋ฐฐ์—ด์ด ๊ฐ€์ง„ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ Linked List์ž…๋‹ˆ๋‹ค.

 

๐Ÿ‰ Linked List ์žฅ๋‹จ์  

Linked List์— ํŠน์ • ๊ฐ’์„ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€ํ•  ๋•Œ ์œ„์™€ ๊ฐ™์ด ๋ณ„๋„์˜ ์ž‘์—…์„ ํ•ด์ฃผ๋Š” ๋กœ์ง์ด ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค.

 

๐ŸŠ Linked List ๊ตฌํ˜„ (with Python) 

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

Node ๊ตฌํ˜„

 

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

    def add(self,data):
        if self.head == None:
            self.head == Node(data)
        else:
            node = self.head
            while node.next:
                node = node.next
            node.next = Node(data)

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

    def delete(self,data):
        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

Linked List ๊ธฐ๋Šฅ ๊ตฌํ˜„

 

 

linkedlist1 = LinkedList(0)
for data in range(2, 10):
    linkedlist1.add(data)
linkedlist1.print()

linkedlist1.delete(4)
print("")
linkedlist1.print()

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๋ฐ ์‹คํ–‰ ๊ฒฐ๊ณผ

'Data Structure' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Heap (ํž™) ์ •๋ฆฌ  (0) 2021.07.28
Tree (ํŠธ๋ฆฌ) ์ •๋ฆฌ  (0) 2021.07.27
Hash Table (ํ•ด์‹œ ํ…Œ์ด๋ธ”) ์ •๋ฆฌ  (0) 2021.07.23
Stack (์Šคํƒ) ์ •๋ฆฌ  (0) 2021.07.22
Queue (ํ) ์ •๋ฆฌ  (0) 2021.07.22
Comments