Click here to Skip to main content
15,915,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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()
Posted
Updated 5-Jan-18 21:07pm
Comments
Jun Jie Oi 6-Jan-18 9:44am    
I have recorrected the code as below..
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 getNext(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.getNext
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()

But I am still seeing an error like below..

Traceback (most recent call last):
File "C:\Users\TP_baseline\Desktop\DSAG\ProjectTest2.py", line 75, in <module>
ll.AddBookAtPosition(book5, 3)
File "C:\Users\TP_baseline\Desktop\DSAG\ProjectTest2.py", line 49, in AddBookAtPosition
head=head.getNext
AttributeError: 'function' object has no attribute 'getNext'

I am not sure if this is the right correction that you had suggested me to take. Any extra guidance would be greatly appreciated.

1 solution

Your class both has an attribute next and a function next. This is not a good idea because, as you can see, collisions happen. Rename either the function or the attribute.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900