-1

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.

2
  • 1
    Try BinaryInsertionSort(l, l.length()) Commented Jun 15, 2020 at 16:39
  • Also, it is a good idea not to overwrite built-in Python functions/keywords, such as list (although it sounds like you didn't write that part of the code). This will eventually cause problems, or at the very least, confusion. Commented Jun 15, 2020 at 16:45

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I changed the call to BinaryInsertionSort(l, l.length()) as Justin Ezequiel said, but somehow it returned 4 5 3 3 3 3 2 1
Well, I guess that means the algorithm does not work properly. If You have a specific question with minimal example, you can create another question. This question was answered. You can accept one of the answers.
1

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 Nodes 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.