简体   繁体   English

Python链表:如何访问下一个值?

[英]Python linked list: how to access the next value?

Here's my code: 这是我的代码:

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


class LinkedList:
    def __init__(self):
        self.first = None
        self.last = None

# then I declare a list and a node
S = LinkedList()
cel = Node
cel = S.first

Now I want to put something in the list: 现在,我想在列表中添加一些内容:

  n = 0
  x = 0
  while n < 5:
      x = input()
      cel.val = x
      cel = cel.next

Yet I get an error stating that: 但是我得到一个错误,指出:

 'NoneType' object has no attribute 'val'
 'NoneType' object has no attribute 'next'

Where is the problem? 问题出在哪儿?

cel is equal to S.first . cel等于S.first S.first is equal to None . S.first等于None When you try to get val from cel m you try to get val attribute of None . 当您尝试从cel m获取val ,尝试获取None val属性。

And none of you classes has val attribute... So it is possible to assign it, but I'd suggest to avoid it, because it is not clear to create it somewhere without declaring it in class itself. 而且你们val都没有val属性...因此可以分配它,但是我建议避免使用它,因为不清楚是否要在类本身中声明它而在某个地方创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM