问题
I've implemented a Linked List class and I have a selectionSort and insertionSort function created that works on regular lists. I need to get selectionSort and insertionSort to work on linked lists, but I'm not sure where to even start, if I'm being honest.
Here's my linked list class:
class Node:
def __init__(self, initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self,newdata):
self.data = newdata
def setNext(self, newnext):
self.next = newnext
class unorderedList:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
def add(self, item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def length(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.getNext()
return count
def search(self, item):
current = self.head
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
current = current.getNext()
return found
def remove(self, item):
current = self.head
previous = None
found = False
while not found:
if current.getData() == item:
found = True
else:
previous = current
current = current.getNext()
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext()
Here's my code for selectionSort and insertionSort. They work just fine on regular lists, but I'm having a lot of trouble figuring out where to start to get them to work on a linkedlist (unordered list).
def selectionSort(alist):
for fillslot in range(len(alist)-1,0,-1):
positionOfMax = 0
for location in range(1,fillslot+1):
if alist[location]>alist[positionOfMax]:
positionOfMax = location
temp = alist[fillslot]
alist[fillslot] = alist[positionOfMax]
alist[positionOfMax] = temp
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position] = alist[position-1]
position = position-1
alist[position] = currentvalue
Any suggestions/hints would be greatly appreciated.
回答1:
I tried to implement insertionSort
, The code is readable. SelectionSort
should be similar, try to implement it.
def insertionSort(h):
if h == None:
return None
#Make the first node the start of the sorted list.
sortedList= h
h=h.next
sortedList.next= None
while h != None:
curr= h
h=h.next
if curr.data<sortedList.data:
#Advance the nodes
curr.next= sortedList
sortedList= curr
else:
#Search list for correct position of current.
search= sortedList
while search.next!= None and curr.data > search.next.data:
search= search.next
#current goes after search.
curr.next= search.next
search.next= curr
return sortedList
def printList(d):
s=''
while d:
s+=str(d.data)+"->"
d=d.next
print s[:-2]
l= unorderedList()
l.add(10)
l.add(12)
l.add(1)
l.add(4)
h= l.head
printList(h)
result= insertionSort(l.head)
d= result
printList(d)
Output:
4->1->12->10
1->4->10->12
来源:https://stackoverflow.com/questions/26705235/how-do-i-implement-selectionsort-and-insertionsort-on-a-linked-list-in-python