当前位置: 首页 > 知识库问答 >
问题:

在链表末尾添加项目会产生奇怪的结果

步博厚
2023-03-14

下面是我的节点类

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()

我得到以下结果,

端部插入20

正在打印节点1-->10-->5-->none->NULL

共有1个答案

禄烨然
2023-03-14

首先用node()初始化head,其中包含none值,这是不正确的,您应该有空head,这就是为什么您在末尾看到none,因为它会传播到末尾,最后,您没有打印最后一个node(),因为最后一个节点没有next这个条件不显示nodenext:

    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 
 类似资料:
  • 我有一个基于泽西(1. x)的REST服务。它使用Jackson 2.4.4生成JSON响应。我需要在响应的末尾添加一个换行符(cURL用户抱怨响应中没有新行)。我使用泽西漂亮打印功能()。 当前: 通缉: > 我尝试使用自定义序列化程序。我只需要在根对象的末尾添加。序列化器是按数据类型定义的,这意味着,如果此类类的实例嵌套在响应中,我将在JSON的中间获得。 我想到了子类化,覆盖,在其中我将添加

  • 问题内容: 我从VOC2012数据集中获取了以下png文件。 我使用以下简单代码: 它产生了以下图像: 奇怪的是,结果不是原始图像的灰度图像。上面的代码还用于deeplab张量流数据集中,以删除真实图像中的颜色图。 有人知道为什么吗?非常感谢! 问题答案: 问题是您的图像已泛化。因此,没有为每个像素存储完整的RGB三元组,而是有一个256个RGB三元组的列表,然后在每个像素位置将索引存储到列表中-

  • 问题内容: 鉴于此程序: Sun的(v 1.6.0_24)产生以下字节码: 带有以下异常表: 我的问题是: 为什么到底在异常表中包括了最后一个条目? 据我了解,它基本上说“ 如果引发异常,请捕获它,然后重试相同的指令 ”。 即使使用空try / catch / finally子句(例如 一些观察 Eclipse编译器不会产生任何此类异常表条目 JVM规范没有记录该指令的任何运行时异常。 我知道JV

  • 问题内容: 我有一个文本区域,其中包含一些文本,我想再次向其中添加一些行(第一行+我要添加的其他行),但是它不起作用。 我现在的操作方式将擦除旧文本并仅显示新行。 问题答案: 代替使用,使用。 将给定的文本追加到文档末尾。如果模型为null或字符串为null或为空,则不执行任何操作。 这会将文字添加到您的末尾。 另一个选择是使用来从中获取文本,然后操作String(添加或删除或更改String),

  • 注意,这个空间只在最末端可见,因为我不想改变布局。我可以使recycerview有一个50dp的边际底部,但我不想这样做。

  • 当我发现这个奇怪的东西时,我正在玩JSX。使用以下JSX: 会产生正确的结果: 但我想在引号周围添加双引号,因此我尝试: 令我惊讶的是,它给出了正确的输出: 我希望得到类似的输出,因为它是字符串文字: 既然在字符串文本中,它为什么不按字面意思告诉我?这是巴贝尔的错误吗? 注意:这是一个自我提问和回答