Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Expected Behavior Write a Python method sort() to sort a LinkedList object in de

ID: 3736227 • Letter: E

Question

Expected Behavior

Write a Python method sort() to sort a LinkedList object in descending order of the attribute _value. The code for the LinkedList class is shown below. You should use the sorting algorithm described here.

Given a LinkedList object llist, the execution of the code

llist.sort()
print(llist)

should cause the sorted list to be printed out.

The code for the LinkedList class is as follows:

Examples

Code:

ll = LinkedList()
ll.add(Node(1))
ll.add(Node(3))
ll.add(Node(2))
ll.sort()
print(ll)

Result: (this is the string returned by the __str__() method for the LinkedList class)

List[ 3; 2; 1; ]

Code:

ll = LinkedList()
ll.sort()
print(ll)

Result:

List[ ]

Code:

ll = LinkedList()
ll = LinkedList()
ll.add(Node(1))
ll.add(Node(1))
ll.add(Node(1))
ll.sort()
print(ll)

Result:

List[ 1; 1; 1; ]

Explanation / Answer

def sort(self): cur = self._head #cur is temporary variable that holds head, so that head remains at first node while True: #infinite loop until flag is false flag = False #flag is used to break the outer infinite loop while cur: #loop iterates till the end of linked list try: #for capturing attribute errors, we are using exception handling if cur._value
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote