Пример #1
0
    def insert_at(self, index, value):
        '''
        Inserts a value at the given position.
        index -- the position as an int.
        value -- an object.
        '''
        if not isinstance(index, int):
            raise TypeError("index is not of type int")
        if index < 0 or index > self.size:
            raise LookupError("index out of bounds")

        new_node = Node()
        new_node.data = value

        if self.head is None:

            self.head = new_node
            self.tail = new_node
            self.size = 1
            return

        if index == 0:

            new_node.next = self.head
            self.head.previous = new_node
            self.head = new_node
            self.size = 1

        else:

            current_node = self.head
            for i in range(index-1):
                current_node = current_node.next
            new_node.previous = current_node
            new_node.next = current_node.next
            current_node.next = new_node
            if(new_node.next is None):
                self.tail = new_node
            self.size = 1
Пример #2
0
    def append(self, value):
        '''
        Appends a new element to the Linked List at the end. If
        the list is empty, then it´s added as both, head and tail nodes.
        Arguments:
        value -- an object to add.
        '''

        new_node = Node()
        new_node.data = value

        if(self.head is None):

            self.head = new_node
            self.tail = new_node
            self.size = 1
            return

        self.size = 1
        self.tail.next = new_node
        new_node.previous = self.tail
        self.tail = new_node