python实现单链表

class Node:
    def __init__(self, value):
        """
        :params: value 值
        :params: next 链接节点
        """
        self.value = value
        self.next = None
    
    def __repr__(self) -> str:
        return f"Node<{self.value}>"
    

class LinkList:
    head: Node = None
    length: int = None

    def create_node(self, value) -> Node:
        return Node(value)
    
    @property
    def is_empty(self):
        """判断链表是否为空"""
        return self.head is None
    
    @property
    def length(self):
        """长度"""
        current_node = self.head
        
        count = 0

        while current_node:
            count += 1
            current_node = current_node.next
        return count

    def search_node_by_value(self, value):
        """按值查找节点"""
        current_node = self.head
        if not current_node:
            return None
        
        while current_node and current_node.value != value:
            current_node = current_node.next

        return current_node
    
    def search_node_by_index(self, position: int):
        """按索引查找节点"""

        current_node = self.head
        if not current_node:
            return None
        
        index = 0
        while current_node and index < position:
            index += 1
            current_node = current_node.next
        
        return current_node


    def update_node_by_index(self, position: int, value):
        """
        按索引修改节点
        :params: position 查找索引
        :params: value 值
        """

        current_node = self.head
        if not current_node:
            return None
        
        index = 0
        while current_node and index < position:
            index += 1
            current_node = current_node.next
        current_node.value = value

        return current_node
    
    def append(self, value):
        """
        从尾部插入节点
        :params: value 值
        """
        current_node = self.head
        node = self.create_node(value)
        while current_node and current_node.next:
            current_node = current_node.next
        current_node.next = node


    def insert(self, position, value):
        """
        从中间插入节点
        :params: position 插入位置
        :params: value 值
        """
        current_node = self.head
        index = 0

        if position > self.length - 1:
            return
        
        node = self.create_node(value)

        while index < position:
            index += 1
            current_node = current_node.next
        node.next = current_node.next
        current_node.next = node


    def add(self, value):
        """
        从头部插入节点
        :params: value 值
        """
        current_node = self.head
        node = self.create_node(value)
        node.next = head.next
        head.next = node


    def remove(self, value):
        """删除"""
        current_node = self.head
        
        pre = None

        while current_node:
            if current_node.value == value:
                # 是否头节点
                if not pre:
                    self.head = current_node.next
                else:
                    pre.next = current_node.next
                    break
            else:
                pre = current_node
                current_node = current_node.next


    def travel(self):
        """
        遍历
        """
        print("*"*20)
        current_node = self.head

        while current_node:
            print(f"{current_node}", end="->")
            current_node = current_node.next
        print()
    
if __name__ == '__main__':
    linklist = LinkList()
    head = linklist.create_node(11)
    linklist.head = head
    linklist.add(41)

    node = linklist.append(21)

    node = linklist.append(31)


    linklist.travel()

    node = linklist.search_node_by_index(1)
    print(node)

    linklist.insert(2, 56)

    linklist.travel()

    node = linklist.update_node_by_index(2, 78)
    print(node)

    linklist.travel()

    linklist.remove(56)
    linklist.travel()

 

posted @ 2023-06-30 16:19  琵琶真的行  阅读(5)  评论(0编辑  收藏  举报