0

I am working on a doubly linked list and I can't get my remove function to work correctly. I want to return the value that it is being removed. I have tried various times, with no success. An extra pair of eye would be appreciated.

 class DoublyLinked_List:

    class __Node:

       def __init__(self, val):
          self.val = val
          self.next = None
          self.prev = None


    def __init__(self):
       self.__header = self.__Node(None)
       self.__trailer = self.__Node(None)
       self.__header.next = self.__trailer
       self.__trailer.prev = self.__header
       self.__size = 0

    def __len__(self):
       return self.__size

    def remove_element_at(self, index):
        if index > self.__size or index < 0 or index == self.__size:
             raise IndexError
        current = self.__header.next
        if index == 0:
            self.__header.next = self.__header.next.next
        else:
            for i in range(0, index-1):
                current = current.next
            current.next = current.next.next
        self.__size -= 1
        return current.val
  • Is there any reason why you are using double preceding underscores instead of one (or none)? https://stackoverflow.com/a/7456865/8079103 – G_M Mar 18 '18 at 15:33
  • Can you write a function that returns a printable representation of the linked list? It should be easier to see what items are getting removed using that. – castle-bravo Mar 18 '18 at 15:36
  • We have to make the attributes private from our user. That's why we are using double underscores. –  Mar 18 '18 at 15:40

0 Answers0