python: Linked List

 

# encoding: utf-8
# 版权所有 2024 涂聚文有限公司
# 许可信息查看:
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# Datetime  : 2024/10/12 6:29
# User      : geovindu
# Product   : PyCharm
# File      : common/LinkedList.py



class Node:
    """
    Create a Node class to create a node
    """
    def __init__(self, data):
        """
        
        :param data: 
        """
        self.data = data  # 资料
        self.next = None  # 下一条 指标



class LinkedList:
    """
    Create a LinkedList class
    """
    def __init__(self):
        """

        """
        self.head = None

   
    def insertAtBegin(self, data: object):
        """
        Method to add a node at begin of LinkedList
        最前端插入节点
        :param data: 
        :return: 
        """
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            return
        else:
            new_node.next = self.head
            self.head = new_node


    def insertAtIndex(self, data: object, index:int):
        """
        Method to add a node at any index
        Indexing starts from 0.
        在指定索引位置插入节点
        :param data: 
        :param index: 
        :return: 
        """
        if (index == 0):
            self.insertAtBegin(data)

        position = 0
        currentNode = self.head
        while (currentNode != None and position + 1 != index):
            position = position + 1
            currentNode = currentNode.next

        if currentNode != None:
            newNode = Node(data)
            newNode.next = currentNode.next
            currentNode.next = newNode
        else:
            print("Index not present")

 
    def insertAtEnd(self, data: object):
        """
        Method to add a node at the end of LinkedList
        在最后位置插入节点
        :param data: 
        :return: 
        """
        
        newNode = Node(data)
        if self.head is None:
            self.head = newNode
            return

        currentNode = self.head
        while (currentNode.next):
            currentNode = currentNode.next

        currentNode.next = newNode

  
    def updateNode(self, val:object, index:int):
        """
        Update node of a linked list
        at given position
        在指定索引位置添加节点
        :param val: 
        :param index: 
        :return: 
        """
        
        currentNode = self.head
        position = 0
        if position == index:
            currentNode.data = val
        else:
            while (currentNode != None and position != index):
                position = position + 1
                currentNode = currentNode.next

            if currentNode != None:
                currentNode.data = val
            else:
                print("Index not present")



    def removeFirstNode(self):
        """
        Method to remove first node of linked list
        删除第一个节点
        :return: 
        """
        if (self.head == None):
            return

        self.head = self.head.next

 
    def removeLastNode(self):
        """
        Method to remove last node of linked list
        删除最后一个节点
        :return: 
        """
        
        if self.head is None:
            return

        currentNode = self.head
        while (currentNode != None and currentNode.next.next != None):
            currentNode = currentNode.next

        currentNode.next = None


    def removeAtIndex(self, index:int):
        """
        Method to remove at given index
        删除指定索引位置的节点
        :param index: 
        :return: 
        """
        if self.head == None:
            return

        currentNode = self.head
        position = 0
        if position == index:
            self.removeFirstNode()
        else:
            while (currentNode != None and position + 1 != index):
                position = position + 1
                currentNode = currentNode.next

            if currentNode != None:
                currentNode.next = currentNode.next.next
            else:
                print("Index not present")


    def removeNode(self, data: object):
        """
        Method to remove a node from linked list
        移除所有节点
        :param data: 
        :return: 
        """
        currentNode = self.head

        if currentNode.data == data:
            self.removeFirstNode()
            return

        while (currentNode != None and currentNode.next.data != data):
            currentNode = currentNode.next

        if currentNode == None:
            return
        else:
            currentNode.next = currentNode.next.next


    def sizeOfLenght(self):
        """
        Print the size of linked list
        :return: 
        """
        
        size = 0
        if (self.head):
            currentNode = self.head
            while (currentNode):
                size = size + 1
                currentNode = currentNode.next
            return size
        else:
            return 0


    def printList(self):
        """
        print method for the linked list
        :return: 
        """
        currentNode = self.head
        while (currentNode):
            print(currentNode.data)
            currentNode = currentNode.next

# test
''''''
# create a new linked list
llist = LinkedList()

# add nodes to the linked list
llist.insertAtEnd('GeovinDu')
llist.insertAtEnd('SibodDu')
llist.insertAtBegin('GinhonZhao')
llist.insertAtEnd('WeiTu')
llist.insertAtIndex('HongTu', 2)

# print the linked list
print("Node Data")
llist.printList()

# remove a nodes from the linked list
print("\nRemove First Node")
llist.removeFirstNode()
print("Remove Last Node")
llist.removeLastNode()
print("Remove Node at Index 1")
llist.removeAtIndex(1)

# print the linked list again
print("\nLinked list after removing a node:")
llist.printList()

print("\nUpdate node Value")
llist.updateNode('z', 0)
llist.printList()

print("\nSize of linked list :", end=" ")
print(llist.sizeOfLenght())

  

posted @ 2024-10-12 06:30  ®Geovin Du Dream Park™  阅读(6)  评论(0编辑  收藏  举报