Fun with bool-- don't do this

I realized this is completely overboard but wanted to share…
I think zeds solution is like… 15 lines with detach node included… pfffft.

def remove(self, obj):
        

        # first cover the beginning of the list, and if there are 0 or only 1 element.
        node = self.begin
        tail = self.end
        if node == None:
            print(" *** Empty list *** ")
            return None
        elif node and not tail:
            if node.value == obj:
                print(" *** The last of his kind... *** ")
                removed = node.value
                self.begin = None
                # for teststing :
                return removed
            else:
                print(" not in list ")
                return None
        # we need to check if they are pop()ing. 
        elif tail.prev == node:
            if obj == tail.value:
                print(" Soda! ")
                removed = tail.value
                self.pop()
                return removed
            elif obj == node.value:
                print(" there she goes. ")
                removed = node.value
                newnode = self.end
                newnode.prev = None
                self.end = None
                self.begin = newnode
                return removed
            else:
                print(" not in list ")
                return None


        # now cover if there are more then one items in list:
        # and it's not any of the things above. 
        else:
            #tail = self.end
            head = self.begin
      ############  issue #############
      ### what if there are multiple matching values? ####
            # from tail <----
                # if obj == node.value.....
                # tail.prev = node.prev
            # from head ---->
                # if obj == node.value
                # head.next = node.next
       ########  You can't do it from both sides... ####
       #### you have to do it from one side only #######
       # [(/), 1DATA , 2--->][<---1, 2DATA 3--->][<----2, 3DATA, (/)]
       # [(/), 1DATA , 3--->][<---1 3DATA, (/)]
            
            # check the begin
            head = self.begin
            if head.value == obj:
                print(" that saved some time. let's have a shift!")
                removed = head.value
                newnode = head.next
                newnode.prev = None
                self.begin = newnode
                return removed

            
            else:
                
                
                linkback = None
                linkup = None
                found = False
                final = False
                removed = " "
                while head:
                    
                    if head.value == obj and not final and not found:
                        if head == self.end:
                            removed = head.value
                            print(" PoP! I went through that whole list....")
                            self.pop()
                            return removed
                        else:
                            linkup = head.next
                            print(linkup)
                            linkback = head.prev
                            print(linkback)
                            removed = head.value
                            found = True
                            head = head.next
                    elif found and not final:
                        # this should be true after the obj is found
                        if head.prev.value == obj:
                            head.prev = linkback
                            # This is probably a crazy wrong way to do this....
                            final = True
                            head = head.prev
                            # go back to the node before obj was found
                        else:
                            # this should not happen.
                            # else continue through list
                            head = head.next
                    elif final:
                        # we should be on the node before the obj was found
                        head.next = linkup
                        print(head)
                        print("final executed.")
                        return removed
                        break
                    else:
                        head = head.next

Yes, I think you should create a remove_node() function for this that just does the dance of taking a node out, and then you’ll have an easier time with this.

1 Like