pythonds
pythonds copied to clipboard
Problem in active code of unordered list
Error reported in course pythonds on page ImplementinganUnorderedListLinkedLists by user anonymous [email protected] The Complete UnorderedList Class (unorderedlistcomplete) This active code has error in the remove function. When you supply to a value to remove function that is not in the list, it gives a syntax error. The condition where current becomes equal to None is not handled. Maybe there should be message like "Not found"
Corrected function:
def remove(self,item):
current = self.head
previous = None
found = False
while current and not found:
if current.getData() == item:
found = True
else:
previous = current
current = current.getNext()
if found:
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())