コード例 #1
0
 def insert_at_begining(self, data):
     new_node = Node(data)
     if self.get_head() == None and self.get_tail() == None:
         self.set_head(new_node)
         self.set_tail(new_node)
     else:
         new_node.set_next(self.get_head())
         self.set_head(new_node)
コード例 #2
0
 def insert_at_middle(self, pos, data):
     if pos < 1:
         print("Position does not exist")
         return None
     else:
         if pos == 1:
             self.insert_at_begining(data)
         elif pos == self.length_linkedlist() + 1:
             self.insert_at_end(data)
         elif pos > self.length_linkedlist() + 1:
             print("Position does not exist")
         else:
             new_node = Node(data)
             temp = self.get_head()
             i = 1
             while i < pos - 1:
                 temp = temp.get_next()
                 i += 1
             new_node.set_next(temp.get_next())
             temp.set_next(new_node)