单链表python不工作的Add Node函数

2024-04-24 02:42:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我是个笨蛋,很难理解和实现一个单链链表,它在尾部添加条目。我相信唯一不起作用的代码是add函数,我不知道它的逻辑。我想我想将第一个节点设置为head,然后在尾部插入其他元素,在添加时将head的指针更改为指向第二个项目,然后将第二个项目的指针指向第三个项目等等,但无法确定如何进行编码(处理未知数量的字符串,为了简单起见,这里有3个。在

strings = ["one", "two", "three"]


class Node:
    def __init__(self,data,nextNode=None):
        # populate the Node, with data and pointer
        self.data = data
        self.nextNode = nextNode

    def getData(self):
        # method to get value of this node
        return self.data

    def setData(self,val):
        # set value of node to val
        self.data = val

    def getNextNode(self):
        # get the pointer to the next node
        return self.nextNode

    def setNextNode(self,val):
        # set pointer to the next node
        self.nextNode = val

class LinkedList:

    def __init__(self, head = None, tail = None):
        # initial properties of linked list, size 0
        self.head = head
        self.tail = tail
        self.size = 0

    def getSize(self):
        # get size of linked list
        return self.size

    def addNode(self,data):
        # Head should point to first node, which will have a value, and a Null pointer
        if (self.size == 0):
            newNode = Node(data, self.tail)
            self.head.getNextNode() = newNode
        else:
        # All additional nodes should be inserted at tail, and with the pointers for the prior nodes changed to point to the new node
            newNode = Node(data, self.tail)
            self.tail = newNode
            self.size += 1
            return True

    def printNode(self):
        curr = self.head
        while curr:
            print(curr.data)#, curr.nextNode)
            curr = curr.getNextNode()

mylist = LinkedList()

for i in strings:
    mylist.addNode(i)

mylist.printNode()

# desired output: Head -> one --> two --> three/Tail

Tags: ofthetoselfnodedatasizedef
1条回答
网友
1楼 · 发布于 2024-04-24 02:42:56

有很多小错误,请在下面的代码中找到它们。如果你不明白什么就告诉我。在

一个重要的变化是新节点不应该访问下一个节点。它已经是最后一个节点,所以它旁边不能有任何节点。另外请密切注意addNode函数的else块。在

strings = ["one", "two", "three","four","five"]

class Node:
    def __init__(self,data):
        # populate the Node, with data and pointer
        self.data = data
        self.nextNode = None

    def getData(self):
        # method to get value of this node
        return self.data

    def setData(self,val):
        # set value of node to val
        self.data = val

    def getNextNode(self):
        # get the pointer to the next node
        return self.nextNode

    def setNextNode(self,val):
        # set pointer to the next node
        self.nextNode = val

class LinkedList:

    def __init__(self, head = None, tail = None):
        # initial properties of linked list, size 0
        self.head = head
        self.tail = tail
        self.size = 0

    def getSize(self):
        # get size of linked list
        return self.size

    def addNode(self,data):
        # Head should point to first node, which will have a value, and a Null pointer
        if (self.size == 0):
            self.head = Node(data)
            self.tail = self.head
            self.size = 1
        else:
        # All additional nodes should be inserted at tail, and with the pointers for the prior nodes changed to point to the new node
            newNode = Node(data)
            self.tail.nextNode = newNode
            self.tail = newNode
            self.size += 1
            return True

    def printNode(self):
        curr = self.head
        while curr:
            print(curr.data)#, curr.nextNode)
            curr = curr.getNextNode()

mylist = LinkedList()

for i in strings:
    mylist.addNode(i)

mylist.printNode()

相关问题 更多 >