Beispiel #1
0
    def insert_at_index(self, index, data):
        if index == 1:
            new_node = Node(data)
            new_node.next = self.head
            self.head = new_node
        if index > 1:
            i = 1
            while i < index - 1 and self.head:
                i = i + 1
                self.head = self.head.next

            if self.head is None:
                print("Index out of bound")
                return
            new_node = Node(data)
            new_node.next = self.head.next
            self.head.next = new_node
Beispiel #2
0
 def insert_before_item(self, x, data):
     if self.head is None:
         print("List has no element")
         return
     if self.head.data == x:
         new_node = Node(data)
         new_node.next = self.head
         self.head = new_node
     while self.head.next:
         if self.head.next.data == x:
             break
         self.head = self.head.next
     if self.head.next is None:
         print("Item doesn't exist")
         return
     new_node = Node(data)
     new_node.next = self.head.next
     self.head.next = new_node
Beispiel #3
0
    def prepend(self, value):
        """
        Adds the value to the start of the list

        :param value: the value to be added
        """
        node = Node(value)
        node.next = self.head
        self.head = node
Beispiel #4
0
 def insert_after_item(self, x, data):
     new_node = Node(data)
     while self.head.data != x and self.head:
         self.head = self.head.next
     if self.head is None:
         print("Item doesn't exist")
         return
     after_node = self.head.next
     self.head.next = new_node
     new_node.next = after_node
Beispiel #5
0
    def prepend(self, value):
        """
        Adds the value to the start of the list

        :param value: the value to be added
        """
        new_node = Node(value)
        current_node = self.head
        new_node.next = current_node
        self.head = new_node
 def prepend(self, value):
     node = Node(value)
     node.next = self.head
     self.head.previous = node
     self.head = node
Beispiel #7
0
 def insert_at_start(self, data):
     new_node = Node(data)
     new_node.next = self.head
     self.head = new_node