The assignment is to use selection sort by swapping nodes, not the values. When sorting algorithm changes the last node, it raises AttributeError, because it seems that first.next in def min_sort()
does not seem to change and is None. Here is the code
class LinkedList:
class Node:
def __init__(self, data, next=None):
self.data, self.next = data, next
def __init__(self, seq):
self.front = self.Node(None)
curr = self.front
for value in seq:
curr.next = self.Node(value)
curr = curr.next
def swap(self, x, y):
first = self.front
while first.next is not None:
if first.next.data == x.data:
prevx = first
if first.next.data == y.data:
prevy = first
first = first.next
x, y = y, x
prevx.next, prevy.next = x, y
temp = x.next
x.next = y.next
y.next = temp
def progress(self, first, reverse):
try:
solid = first
while first.next is not None:
if solid.data > first.next.data and not reverse:
self.swap(solid, first.next)
elif solid.data < first.next.data and reverse:
self.swap(solid, first.next)
first = first.next
except Exception:
pass
def min_sort(self, reverse):
first = self.front
while first.next is not None:
self.progress(first, reverse)
first = first.next
I don’t know how to change the reference in function min_sort
so it would work
New contributor
Samuel Varchol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.