删除方法链接列表

时间:2018-03-18 15:27:13

标签: python python-3.x return doubly-linked-list

我正在制作一个双向链表,我无法使我的删除功能正常工作。我想返回它被删除的值。我尝试了很多次,没有成功。额外的一双眼睛将不胜感激。

 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

0 个答案:

没有答案