【问题标题】:Add items to end of Linked List gives strange result将项目添加到链接列表的末尾会产生奇怪的结果
【发布时间】:2018-08-02 20:02:13
【问题描述】:

我正在尝试将元素添加到链接列表的末尾,但没有看到这种情况发生。它似乎总是将 None 添加到链表的末尾。我不确定为什么会发生这种情况,我猜这与我的 .next 设置不正确有关。我正在使用 python 来实现我的链表。有人可以帮我解决这个问题吗

下面是我的节点类

class Node: 
    def __init__(self):
        self.data = None
        self.next = None
    
    def get_data(self):
        return self.data
    
    def set_data(self, data):
        self.data = data
        
    def get_next(self):
        return self.next
    
    def set_next(self, node):
        self.next = node

这是我的带有插入方法的链表类

class SingleyLinkedList:
    
    def __init__(self):
        self.head = Node()
    
    def insertAtHead(self, data):
        currentNode = self.head

        newNode = Node()
        newNode.set_data(data)  
        
        if currentNode != None:
            newNode.set_next(currentNode)
            self.head = newNode
            print("Inserted ", data, " at the head")
        else:
            self.head.set_next(newNode)
    
    def insertAtEnd(self, data):
        currentNode = self.head
        
        new_node = Node()
        new_node.set_data(data)
        
        while currentNode.get_next() != None:
            currentNode = currentNode.next
        
        currentNode.set_next(new_node)
        print("Inserted ", data, " at end")
    
    def printNode(self):
        print("\nPrinting the nodes")
        currentNode = self.head
        
        while currentNode.next != None:
            print(currentNode.data, " --> ", end="")
            currentNode = currentNode.next
        print(" NULL \n")

s = SingleyLinkedList()

s.insertAtHead(5)
s.printNode()

s.insertAtHead(10)
s.printNode()

s.insertAtHead(1)
s.printNode()

s.insertAtEnd(20)
s.printNode()

我得到以下结果,

在头部插入 5

打印节点 5 --> 空

在头部插入 10

打印节点 10 --> 5 --> 空

在头部插入 1

打印节点 1 --> 10 --> 5 --> NULL

在末尾插入 20

打印节点 1 --> 10 --> 5 --> 无 --> NULL

【问题讨论】:

  • 与问题无关,但init中的self.head = Node()必须是self.head = None
  • 嘿,DYZ,我已经用self.head=None() 来初始化一个空节点,同时使用self.data=Noneself.next=None。这样我只需要给数据赋值,然后下一步

标签: python data-structures linked-list


【解决方案1】:

首先使用Node() 初始化头,其中包含不正确的None 值,您应该有空头,这就是为什么您在最后看到None,因为它会传播到最后,最后您不打印最后一个Node() 因为最后一个节点没有next 这种情况不显示Node 没有下一个:

    while currentNode.next != None:
        print(currentNode.data, " --> ", end="")
        currentNode = currentNode.next

所以你需要再打印一张。

下面的工作示例稍作简化:

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

    def get_data(self):
        return self.data

    def set_data(self, data):
        self.data = data

    def get_next(self):
        return self.next

    def set_next(self, node):
        self.next = node


class SingleyLinkedList:

    def __init__(self):
        self.head = None

    def insertAtHead(self, data):
        old_head = self.head
        self.head = Node(data, old_head)
        print("Inserted ", data, " at the head")


    def insertAtEnd(self, data):
        currentNode = self.head

        while currentNode.get_next() != None:
            currentNode = currentNode.next

        currentNode.set_next(Node(data))
        print("Inserted ", data, " at end")


    def printNode(self):
        print("\nPrinting the nodes")
        currentNode = self.head

        while currentNode.next != None:
            print(currentNode.data, " --> ", end="")
            currentNode = currentNode.next
        print(currentNode.data, " --> ", end="")
        print(" NULL \n")


s = SingleyLinkedList()

s.insertAtHead(5)
s.printNode()

s.insertAtHead(10)
s.printNode()

s.insertAtHead(1)
s.printNode()

s.insertAtEnd(20)
s.printNode()

输出:

Inserted  5  at the head

Printing the nodes
5  -->  NULL 

Inserted  10  at the head

Printing the nodes
10  --> 5  -->  NULL 

Inserted  1  at the head

Printing the nodes
1  --> 10  --> 5  -->  NULL 

Inserted  20  at end

Printing the nodes
1  --> 10  --> 5  --> 20  -->  NULL 

【讨论】:

    【解决方案2】:

    您的insertAtEnd 是正确的,但您的printNode 是不正确的。事实上,它总是用NULL 替换链表中的最后一项。当您将项目添加到列表的开头时,这很好,因为它会抑制您初始化链接列表的空 head 节点。但是当您在列表末尾插入项目时,该虚拟节点现在位于列表中间,并且在您打印时将显示为None

    s = SingleyLinkedList()
    s.insertAtEnd(1)
    s.insertAtEnd(2)
    s.insertAtEnd(3)
    

    打印

    Printing the nodes
    None  --> 1  --> 2  -->  NULL
    

    解决方案:初始化self.head = None,并在您的方法中适当地处理这种情况。

    【讨论】:

    • 您的回答不完整,代码片段还有其他问题
    【解决方案3】:

    您的代码中的问题是:-

    self.head = Node()

    即当您实例化 SingleyLinkedList 类时。您正在将链表对象的头部分配给下一个为 None 的此类节点。

    因此,您在打印语句 Printing the nodes 1 --> 10 --> 5 --> None --> NULL 中得到 None

    下面是正确的代码:

    class Node:
        def __init__(self):
            self.data = None
            self.next = None
    
        def get_data(self):
            return self.data
    
        def set_data(self, data):
            self.data = data
    
        def get_next(self):
            return self.next
    
        def set_next(self, node):
            self.next = node
    
    class SingleyLinkedList:
    
        def __init__(self):
            self.head = None
    
        def insertAtHead(self, data):
            newNode = Node()
            newNode.set_data(data)
    
            if self.head:
                newNode.next = self.head
                self.head = newNode
    
            else:
                self.head = newNode
            print "Inserted ", data, " at the head"
    
        def insertAtEnd(self, data):
            currentNode = self.head
    
            new_node = Node()
            new_node.set_data(data)
    
            while currentNode.get_next() != None:
                currentNode = currentNode.next
    
            currentNode.set_next(new_node)
            print("Inserted ", data, " at end")
    
        def printNode(self):
            print("\nPrinting the nodes")
            currentNode = self.head
    
            while currentNode != None :
                print currentNode.data,
                print " --> ",
                currentNode = currentNode.next
            print
    
    s = SingleyLinkedList()
    
    s.insertAtHead(5)
    s.printNode()
    
    s.insertAtHead(10)
    s.printNode()
    
    s.insertAtHead(1)
    s.printNode()
    
    s.insertAtEnd(20)
    s.printNode()
    

    输出是:

    Inserted  5  at the head
    
    Printing the nodes
    5  --> 
    Inserted  10  at the head
    
    Printing the nodes
    10  -->  5  --> 
    Inserted  1  at the head
    
    Printing the nodes
    1  -->  10  -->  5  --> 
    Inserted  20  at end
    
    Printing the nodes
    1  -->  10  -->  5  -->  20  --> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 2018-07-27
      • 2022-01-08
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      相关资源
      最近更新 更多