Reputation: 137
I created a class called LinkedList and a method inside this class called find().
I'm trying to call this method inside a function but it always return the following error:
Traceback (most recent call last):
File "main.py", line 44, in <module>
BinaryInsertionSort(l.head, l.length())
File "main.py", line 26, in BinaryInsertionSort
item = list.find(i).data
AttributeError: 'Node' object has no attribute 'find'
I have another class called Node, but I don't think this would influence anything.
Please, someone help me! I don't know what is wrong....
Here is the link for the entire code: https://repl.it/@Mimik1/InsertionSortWithLinkedList#main.py
It's for a university project so I can't change the Binary Insertion sort at all, but if it is wrong, please tell me.
Upvotes: -1
Views: 73
Reputation: 470
In LinkedList.py in method add you assign a Node object to the member head. Which is later given to BinaryInsertionSort
which expects an Object of type LinkedList not Node. So the problem is the call BinaryInsertionSort(l.head, l.length())
in main.py line 44. The call to the class method by item = list.find(i).data
is alright.
Upvotes: 1
Reputation: 3120
Your class structure states that a linked list holds a chain of nodes. Therefore, when you call l.head
you return a Node
and not another LinkedList
which is why you are running into the error. A Node
does not have the operations that a LinkedList
has. You could design your LinkedList
in such a way that it is comprised of itself at each link in the chain rather than comprised of Node
s which would alleviate this issue.
Some other comments outside of the scope of the question:
I noticed that you set some integer type arguments to None
as default. Generally, it would seem to be better to set them to a default integer (like 0 in main.py line 23).
I would also look at your logic for adding a node because intuitively it seems like the head should not change as I add more nodes. The head of the list is unchanging whereas I would assume the tail of the list is always the last node.
Upvotes: 1