Ejemplo n.º 1
0
    def insert_after(self, prev_node, data):
        """
        Inserts a new node after the prev_node.

        Parameters
        ==========

        prev_node: LinkedListNode
            The node after which the
            new node is to be inserted.

        data
            Any valid data to be stored in the node.
        """
        self.size += 1
        new_node = LinkedListNode(data,
                                 links=['next', 'prev'],
                                 addrs=[None, None])
        new_node.next = prev_node.next
        if new_node.next is not None:
            new_node.next.prev = new_node
        prev_node.next = new_node
        new_node.prev = prev_node

        if new_node.next is None:
            self.tail = new_node
Ejemplo n.º 2
0
    def insert_before(self, next_node, data):
        """
        Inserts a new node before the next_node.

        Parameters
        ==========

        next_node: LinkedListNode
            The node before which the
            new node is to be inserted.

        data
            Any valid data to be stored in the node.
        """
        self.size += 1
        new_node = LinkedListNode(data,
                                 links=['next', 'prev'],
                                 addrs=[None, None])
        new_node.prev = next_node.prev
        next_node.prev = new_node
        new_node.next = next_node
        if new_node.prev is not None:
            new_node.prev.next = new_node
        else:
            self.head = new_node
Ejemplo n.º 3
0
    def insert_at(self, index, key, data=None):
        if self.size == 0 and (index in (0, -1)):
            index = 0

        if index < 0:
            index = self.size + index

        if index > self.size:
            raise IndexError('%d index is out of range.' % (index))

        self.size += 1
        new_node = LinkedListNode(key,
                                  data,
                                  links=['next', 'prev'],
                                  addrs=[None, None])
        if self.size == 1:
            self.head, self.tail = \
                new_node, new_node
        elif index == self.size - 1:
            new_node.prev = self.tail
            new_node.next = self.tail.next
            self.tail.next = new_node
            self.tail = new_node
        else:
            counter = 0
            current_node = self.head
            prev_node = None
            while counter != index:
                prev_node = current_node
                current_node = current_node.next
                counter += 1
            new_node.prev = prev_node
            new_node.next = current_node
            if prev_node is not None:
                prev_node.next = new_node
            if current_node is not None:
                current_node.prev = new_node
            if new_node.next is None:
                self.tail = new_node
            if new_node.prev is None:
                self.head = new_node
 def insert_before(self, next_node, key, data=None):
     self.size += 1
     new_node = LinkedListNode(key, data,
                              links=['next', 'prev'],
                              addrs=[None, None])
     new_node.prev = next_node.prev
     next_node.prev = new_node
     new_node.next = next_node
     if new_node.prev is not None:
         new_node.prev.next = new_node
     else:
         self.head = new_node
    def insert_after(self, prev_node, key, data=None):
        self.size += 1
        new_node = LinkedListNode(key, data,
                                 links=['next', 'prev'],
                                 addrs=[None, None])
        new_node.next = prev_node.next
        if new_node.next is not None:
            new_node.next.prev = new_node
        prev_node.next = new_node
        new_node.prev = prev_node

        if new_node.next is None:
            self.tail = new_node
Ejemplo n.º 6
0
    def insert_at(self, index, key, data=None):
        """
        Inserts a new node at the input index.

        Parameters
        ==========

        index: int
            An integer satisfying python indexing properties.

        data
            Any valid data to be stored in the node.
        """
        if self.size == 0 and (index in (0, -1)):
            index = 0

        if index < 0:
            index = self.size + index

        if index > self.size:
            raise IndexError('%d index is out of range.' % (index))

        self.size += 1
        new_node = LinkedListNode(key,
                                  data,
                                  links=['next', 'prev'],
                                  addrs=[None, None])
        if self.size == 1:
            self.head, self.tail = \
                new_node, new_node
        else:
            counter = 0
            current_node = self.head
            prev_node = None
            while counter != index:
                prev_node = current_node
                current_node = current_node.next
                counter += 1
            new_node.prev = prev_node
            new_node.next = current_node
            if prev_node is not None:
                prev_node.next = new_node
            if current_node is not None:
                current_node.prev = new_node
            if new_node.next is None:
                self.tail = new_node
            if new_node.prev is None:
                self.head = new_node