본문 바로가기

과목/자료구조

파이썬 연결 리스트 singly linked list

1. 지정 노드의 왼쪽 또는 오른쪽에 삽입하는 것 구현

2. node 자체를 찾는 함수

3. node의 data를 찾는 함수

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


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

    def append(self, data):
        node = Node(data)
        if self.head is None:  # 노드가 없는 경우
            self.head = node
            self.current = node
        else:  # 노드가 있는 경우
            self.current = self.head
            while self.current.next:
                self.current = self.current.next
            self.current.next = node
        return node

    def insert_to_right(self, data, to):
        node = Node(data)
        self.current = self.head
        while self.current != to:
            if self.current is None:
                return '기준 노드를 찾지 못함(right)'
            self.current = self.current.next
        # to 노드를 찾으면 to 노드 오른쪽에 삽입
        node.next = self.current.next
        self.current.next = node
        return node

    def insert_to_left(self, data, to):
        node = Node(data)
        if self.head == to:
            node.next = self.head
            self.head = node
            return node
        self.pre_node = self.head
        self.current = self.head
        while self.current != to:
            if self.current is None:
                return '기준 노드를 찾지 못함(left)'
            self.pre_node = self.current
            self.current = self.current.next
        # to 노드를 찾으면 to 노드 왼쪽에 삽입
        node.next = self.current
        self.pre_node.next = node
        return node

    def show_nodes(self):
        if self.head is None:
            return '노드가 없음'
        else:
            self.current = self.head
        data = []
        while self.current:
            data.append(self.current.data)
            self.current = self.current.next
        return data

    def delete(self, key):
        self.current = self.head
        if self.current.data == key:
            self.head = self.current.next
            self.current = None
            return
        self.pre_node = self.current
        self.current = self.pre_node.next
        while self.current and self.current.data != key:
            self.pre_node = self.pre_node.next
            self.current = self.current.next
        if self.current is None:
            return '지울 노드를 찾지 못함'
        self.pre_node.next = self.current.next
        self.current = None
        return

    def search_node(self, obj):
        self.current = self.head
        while self.current != obj:
            if self.current is None:
                return '노드를 찾지 못함'
            self.current = self.current.next
        return self.current

    def search_data(self, data):
        self.current = self.head
        while self.current is not None and self.current.data != data:
            self.current = self.current.next
        if self.current is None:
            return '원하는 값을 찾지 못함'
        return data


linked_list = LinkedList()

meaningless = 999
a = linked_list.append(1)
print('모든 노드 데이터: ', linked_list.show_nodes())
b = linked_list.insert_to_left(3, a)
print('모든 노드 데이터: ', linked_list.show_nodes())
c = linked_list.insert_to_right(5, a)
print('모든 노드 데이터: ', linked_list.show_nodes())
linked_list.delete(1)
print(linked_list.delete(4))
print('모든 노드 데이터: ', linked_list.show_nodes())
d = linked_list.append(7)
print('모든 노드 데이터: ', linked_list.show_nodes())
e = linked_list.insert_to_left(9, b)
print('모든 노드 데이터: ', linked_list.show_nodes())
f = linked_list.insert_to_right(11, e)
print('모든 노드 데이터: ', linked_list.show_nodes())
print('노드 찾기: ', linked_list.search_node(a))
print('노드 찾기: ', linked_list.search_node(meaningless))
print('데이터 찾기: ', linked_list.search_data(5))
print('데이터 찾기: ', linked_list.search_data(0))
print('삭제된 노드:', linked_list.delete(meaningless))
print('삭제된 노드:', linked_list.delete(9))
print('모든 노드 데이터: ', linked_list.show_nodes())
print('삭제된 노드:', linked_list.delete(7))
print('모든 노드 데이터: ', linked_list.show_nodes())
g = linked_list.insert_to_right(55, meaningless)
h = linked_list.insert_to_right(-1, c)
print('모든 노드 데이터: ', linked_list.show_nodes())
i = linked_list.insert_to_left(-10, c)
print('모든 노드 데이터: ', linked_list.show_nodes())
j = linked_list.insert_to_left(-20, c)
print('모든 노드 데이터: ', linked_list.show_nodes())
k = linked_list.insert_to_left(-30, h)
print('모든 노드 데이터: ', linked_list.show_nodes())
l = linked_list.delete(-10)
print('모든 노드 데이터: ', linked_list.show_nodes())

 

 

 

'과목 > 자료구조' 카테고리의 다른 글

해시 해쉬 해시테이블 해쉬테이블 Hash Hash Table  (0) 2019.02.21