コード例 #1
0
 def append(self, item, number_of_columns=0):
     """Insert the given item at the tail of this linked list.
     TODO: Running time: O(1) Why and under what conditions?"""
     # TODO: Create new node to hold given item
     node = Node(item)
     if type(item) is list:
         node.data = LinkedList(node.data)
         # TODO: Append node after tail, if it exists
         if self.tail is None:
             self.head = node
             self.tail = node
         else:
             self.tail.next = node
             self.tail = node
     else:
         # start from first column
         columns = 0
         for node in self:
             if columns == number_of_columns:
                 linkedlist = node.data
                 linkedlist.append(item)
                 break
             else:
                 # count columns
                 columns += 1
コード例 #2
0
 def prepend(self, item, number_of_columns):
     """Insert the given item at the head of this linked list.
     TODO: Running time: O(1) Why and under what conditions?"""
     # TODO: Create new node to hold given item
     node = Node(item)
     if type(item) is list:
         node.data = LinkedList(node.data)
         # TODO: Prepend node before head, if it exists
         if self.is_empty():
             self.head = node
             self.tail = node
         else:
             node.next = self.head
             self.head = node
     else:
         columns = 0
         for node in self:
             if columns == number_of_columns:
                 linkedlist = node.data
                 linkedlist.prepend(item)
                 break
             else:
                 # count columns
                 columns += 1