I am trying to write a program in Python as part of an assignment and I needed to use LinkedList and Nodes To Solve The Question. However when I try to run the program I am getting an error message as such:
Traceback (most recent call last):
File "C:\Users\baseline\Desktop\DSAG\ProjectTest2.py", line 75, in <module>
ll.AddBookAtPosition(book5, 3)
File "C:\Users\baseline\Desktop\DSAG\ProjectTest2.py", line 49, in AddBookAtPosition
head=head.next
AttributeError: 'function' object has no attribute 'next'
Any Help Or Advice To Guide Me In The Right Direction Would Be Greatly Appreciated
What I have tried:
This Is My Code That I have written so far
<pre>class Node :
def __init__(self, newData):
self.data = newData
self.next = None
def getData(self):
return self.data
def setData(self,newData):
self.data = newData
def getNext(self):
return self.next
def next(self):
return self.next
def setNext(self,newNode):
self.next = newNode
class LinkedList :
def __init__(self):
self.head = None
def AddBookToFront(self,newNode):
newNode.setNext(self.head)
self.head = newNode
def next(self):
return self.next
def insertAfter(self, prev_node, new_data):
if prev_node is None:
return
new_node = Node(new_data)
new_node.next = prev_node.next
prev_node.next = new_node
def AddBookAtPosition(head, newNode, position):
start=head
if position == 0:
return Node(newNode, head)
while position > 1:
head=head.next
position-=1
head.next= Node(newNode, head.next)
return start
def printAll(self):
cursor = self.head
while cursor is not None:
print(cursor.getData())
cursor=cursor.getNext()
book1=("abc", "Peter")
book2=("def", "James")
book3=("ghi", "Sam")
book4=("jkl", "Tom")
book5=("mno", "Jessie")
a = Node(book1)
b = Node(book2)
c = Node(book3)
d = Node(book4)
ll=LinkedList()
ll.AddBookToFront(b)
ll.AddBookToFront(a)
ll.insertAfter(ll.head.next, book4)
ll.AddBookAtPosition(book5, 3)
ll.printAll()