【问题标题】:Problem in traversing a linked list after finding the middle node找到中间节点后遍历链表的问题
【发布时间】:2021-12-07 13:12:45
【问题描述】:

我刚刚开始学习 Python 中的算法和数据结构,我遇到了这个链表问题。总的来说,我设法以天真的方式找到了中间节点,但我不明白为什么在遍历列表后它不会输出最后一个打印命令。

这是遍历函数:

def traverse(self):
        actual_node = self.head

        print("----------")
        while actual_node is not None:
            print(actual_node.data)
            actual_node = actual_node.next
        print("----------") # This is not displayed in the output

这是整个程序:

# Linked List Question
# Construct an algorithm that's able to find the middle node and traverse the list


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

class LinkedList:
    def __init__(self):
        self.head = None
        self.size = 0

    def insert_start(self, data):
        self.size += 1
        new_node = Node(data)

        if not self.head:
            self.head = new_node
        else:
            new_node.next = self.head
            self.head = new_node

    def size_of_linkedList(self):
        return self.size

    def traverse(self):
        actual_node = self.head

        print("----------")
        while actual_node is not None:
            print(actual_node.data)
            actual_node = actual_node.next
        print("----------")



    def middle_node(self):
        actual_node = self.head
        sizeofNode = 0

        while actual_node is not None:
            if sizeofNode == self.size // 2:
                print("This is the middle node: " + str(actual_node))
                print("This is the middle node data: " + str(actual_node.data))
                break
            else:
                actual_node = actual_node.next
                sizeofNode += 1

if __name__ == "__main__":

    linked_list = LinkedList()

    linked_list.insert_start(0)
    linked_list.insert_start(1)
    linked_list.insert_start(2)
    linked_list.insert_start(3)
    linked_list.insert_start(4)
    linked_list.insert_start(5)
    linked_list.insert_start(6)

    print("The size of the list is:  %d" % linked_list.size_of_linkedList())
    linked_list.middle_node()
    linked_list.traverse()

这些是我收到的错误:

Traceback (most recent call last):
  File "linkedListQ1.py", line 66, in <module>
    linked_list.traverse()
  File "linkedListQ1.py", line 33, in traverse
    print(actual_node.data)
AttributeError: type object 'Node' has no attribute 'data'

这是输出:

The size of the list is:  7
This is the middle node: <__main__.Node object at 0x7fb6ee6d1d00>
This is the middle node data: 3
----------
6
5
4
3
2
1
0

除了错误之外,程序的问题是 print("----------") 没有在节点的最后一个元素之后执行。有人可以解释我做错了什么并提供代码sn-ps。提前谢谢你。

【问题讨论】:

  • 这是一个简单的错字 - 在 .__init__ 方法中应该是 self.next = None 而不是 self.next = Node
  • 请注意有趣的错误消息 - 它显示“AttributeError: type object 'Node' has no attribute 'data'”而不是“AttributeError: 'Node' object has no attribute 'data'”。 (嗯;至少我认为这很有趣。)
  • 感谢 cmets,程序现在可以运行了。

标签: python data-structures linked-list traversal


【解决方案1】:

这是一个简单的错字 - 它应该是 self.next = None 而不是 .__init__ method 中的 self.next = Node

注意有趣的错误消息 - 它说 "AttributeError: type object 'Node' has no attribute 'data'" 而不是 "AttributeError: 'Node' object has no attribute 'data '"。 (嗯;至少我认为这很有趣。)

【讨论】:

    猜你喜欢
    • 2011-06-10
    • 2011-01-16
    • 2013-07-28
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多