Reputation: 1897
So for data structure practice in different languages, I decided to create a Linked List in python.
Now here is my code so far, it is still far from done, but I've encountered this error and I cant seem to point out to myself where it is coming from:
class LinkedNode:
def __init__(self, value):
self.value = value
self.nextNode = None
class LinkedList:
def __init__(self):
self.headNode = None
def isEmpty(self):
return self.headNode == None
def addToHead(self, value):
head = LinkedNode(value)
def removeFromHead(self):
head = self.headNode
self.headNode = self.headNode.nextNode
return head
def addToTail(self, value):
if self.headNode == None:
self.addToHead(value)
else:
tmp = self.headNode
while tmp.nextNode != None:
tmp = tmp.nextNode
tmp.nextNode = LinkedNode(value)
def empty(self):
self.headNode = None
def contains(self, value):
tmp = self.headNode
while tmp.nextNode != None:
if tmp.value == value:
return true
tmp = tmp.nextNode
return False
def toString(self):
tmp = self.headNode
buffer = '['
while tmp.nextNode != None:
buffer = buffer + str(tmp.value) + ', '
tmp = tmp.nextNode
buffer = buffer + ']'
list = LinkedList()
list.addToHead(1)
list.addToHead(2)
list.addToTail(3)
list.toString()
Here is the error:
Traceback (most recent call last):
File "path/linkedList.py", line 52, in list.toString()
File "path/linkedList.py", line 44, in toString
while tmp.nextNode != None:
AttributeError: 'NoneType' object has no attribute 'nextNode' [Finished in 0.138s]
Upvotes: 0
Views: 181
Reputation: 3510
You are doing head = LinkedNode(value)
, expecting the LinkedList
's head
to be overwritten. But instead, what happens is that you create a local variable named head
and assign it the value of LinkedNode(value)
. As a result, head
is always None
. And you obviously can't do None.nextNode
(because it ain't an object).
Solution: What you actually need is self.head = LinkedNode(value)
. self
is a reference to the object you're working with.
a. By simply setting the head
to the new node, you lose reference to the previously existing nodes.
Solution: What you need is:
def addToHead(self, value):
tmp = LinkedNode(value)
tmp.nextNode = self.headNode
self.headNode = tmp
b. toString
is a popular way of representing objects as strings, in other programming languages, but the pythonic way of doing it is by overriding the __str__()
and __repr__()
methods for the object.
Solution (I've stuck with your format, although it could use some changing ;):
def __repr__(self):
tmp = self.headNode
buffer = []
while tmp is not None:
buffer.append(tmp.value)
tmp = tmp.nextNode
return "[" + ", ".join(map(str, buffer)) + "]"
c. There are several things wrong about the toString
method (headNode
is not being printed, will fail headNode
being None
etc.), but I'm not gonna try and fix them, as we have the solution above.
Upvotes: 1
Reputation: 111389
Your addToHead method had no side effects. Possibly you meant
def addToHead(self, value):
head = LinkedNode(value)
head.nextNode = self.headNode
self.headNode = head
Also, toString will crash for empty lists.
Upvotes: 1