ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

Linked List?

๋‹จ์ˆœํ•œ ์„ ํ˜• ๋ฐฐ์—ด(๋ฆฌ์ŠคํŠธ)์„ ์„ ์–ธํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์—ฐ์†์ ์œผ๋กœ ํ• ๋‹นํ•œ๋‹ค. ๋งŒ์•ฝ, ๋ฐฐ์—ด ๋‚ด์˜ ๊ฐ’ ์‚ญ์ œ๊ฐ€ ํ•„์š”ํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๊ฐ’๋“ค์˜ ์ž๋ฆฌ ๊ต์ฒด๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ด์™€ ๋‹ฌ๋ฆฌ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ์—ฐ์†์ ์ธ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ๊ฐ๊ฐ์˜ ๋…ธ๋“œ๋“ค์„ ์—ฐ๊ฒฐํ•œ ๊ตฌ์กฐ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์ค‘๊ฐ„์˜ ๊ฐ’ ์‚ญ์ œ๊ฐ€ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ผ์–ด๋‚˜๋Š” ์ž๋ฃŒ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ์— ๋ณด๋‹ค ํšจ์œจ์ ์ด๋‹ค.

 


  •  

Single Linked List

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


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

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

    def delete(self, data):
        if self.head is None:
            return -1

        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
                    break
                else:
                    node = node.next

    def display(self):
        node = self.head
        while node:
            print(node.data)
            node = node.next
  • add, delete, display ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.
  • ๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” next(tail)์„ ํ†ตํ•ด ๊ฐ ๋ฆฌ์ŠคํŠธ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ์  ํ•œ๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
  • ์ด์™ธ์˜ ๊ฐ’์„ ํƒ์ƒ‰ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฉด์œผ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Double Linked List

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


class DoubleLinkedList:
    def __init__(self, data):
        self.head = Node(data)
        self.tail = self.head
        self.num_of_node = 1

    def add_first(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head.prev = new_node
        self.head = new_node
        self.num_of_node += 1

    def add_last(self, data):
        new_node = Node(data)
        new_node.prev = self.tail
        self.tail.next = new_node
        self.tail = new_node
        self.num_of_node += 1

    def delete(self, data):
        if self.head is None:
            return False

        if self.head.data == data:
            temp, self.head = self.head, self.head.next
            del temp
        node = self.head
        while node.next:
            if node.next.data == data:
                temp, node.next = node.next, node.next.next
                node.next.prev = node
                del temp
                return True
            node = node.next
        return False

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

    def display_rev(self):
        node = self.tail
        while node:
            print(node.data)
            node = node.prev

    def ins_front(self, n, data):
        if not n:
            self.add_first(data)
            return True
        elif n == self.num_of_node - 1:
            self.add_last(data)
            return True
        else:
            if self.num_of_node < n:
                return False

            node = self.head.next
            while node.next:
                node = node.next

            new_node = Node(data)
            new_node.next = node.next
            node.next.prev = new_node
            node.next = new_node
            new_node.prev = node
            self.num_of_node += 1
            return True
    
    def ins_back(self, n, data):
        if not n:
            self.add_last(data)
            return True
        elif n == self.num_of_node - 1:
            self.add_first(data)
            return True
        else:
            if self.num_of_node < n:
                return False

            node = self.tail.prev
            while node.next:
                node = node.prev

            new_node = Node(data)
            new_node.prev = node.prev
            node.next.next = new_node
            node.prev = new_node
            new_node.next = node
            self.num_of_node += 1
            return True
  • ๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์™€ ๋‹ฌ๋ฆฌ ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” prev, next๋ฅผ ํ†ตํ•ด ์ •๋ฐฉํ–ฅ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ์ถ”๊ฐ€๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์ด๋ฅผ ์œ„ํ•ด Node ํด๋ž˜์Šค์— prev ๋ฉค๋ฒ„๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , head, prev๋ฅผ ํ†ตํ•ด ์‚ฝ์ž… ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋ฉ”์†Œ๋“œ๋“ค์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€