class node:
def __init__(self):
self.value=None
self.pointer=None
def setvalue(self,x):
self.value=x
def setpointer(self,node):
self.pointer=node
def show(self):
print self.nodes
print self.value
class linkedlist:
def __init__(self):
self.nodes=[]
def addnode(self,nodevalue):
newnode=node()
newnode.setvalue(nodevalue)
self.nodes[-1].setpointer(newnode)
def removenode(self,nodeposition):
if nodeposition==0:
self.nodes[1:]
else:
self.nodes[0:nodepostion:1]+self.nodes[(nodespostion+1):]
item=linkedlist()
item.addnode(2)
Asked
Active
Viewed 45 times
-1

Markku K.
- 3,840
- 19
- 20

user3281113
- 11
-
2What's the issue specifically? – shuttle87 Feb 17 '14 at 20:41
-
Among other things, you access self.nodes in node.print, but you never set self.nodes anywhere else in node. Also, you probably need to `self.nodes.append(newnode)` in linkedlist.addnode(), before you can access self.nodes[-1]. – Markku K. Feb 17 '14 at 20:43
-
@Markku_K thank you, so should I put it all under one class? – user3281113 Feb 17 '14 at 21:30
-
1You can look at some examples here: http://stackoverflow.com/questions/280243/python-linked-list – Markku K. Feb 17 '14 at 22:13
1 Answers
0
You don't have to use this implementation, python lists are easier.
Try creating a list like you did linkedlist.nodes
self.nodes=[]
and add to it by
nodes.append(nodevalue)
When I have executed your code, there is an exception in this line
self.nodes[-1].setpointer(newnode)
lists have no values at negative indices.
For more information about lists, refer to tutorials point - python lists

Abdallah Nasir
- 607
- 6
- 33