John Mayne
John Mayne

Reputation: 277

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?

Upvotes: 0

Views: 1686

Answers (1)

Andrii Rusanov
Andrii Rusanov

Reputation: 4606

cel is equal to S.first. S.first is equal to None. When you try to get val from celm you try to get val attribute of None.

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.

Upvotes: 1

Related Questions