pythonds icon indicating copy to clipboard operation
pythonds copied to clipboard

Problem in active code of unordered list

Open irunestone opened this issue 8 years ago • 1 comments

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"

irunestone avatar Nov 04 '16 02:11 irunestone

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())

victor-ono avatar Sep 10 '18 14:09 victor-ono