예제 #1
0
 def append(self, item):
     current = self.head
     if current:
         while current.getNext() is not None:
             current = current.getNext()
         current.setNext(Node(item))
     else:
         self.head = Node(item)
예제 #2
0
 def insert(self, pos, item):
     node = Node(item)
     if not self.head:
         self.head = node
     elif pos == 0:
         node.next = self.head
         self.head = node
     else:
         previous = None
         current = self.head
         current_position = 0
         while (current_position < pos) and current.getNext():
             previous = current
             current = current.getNext()
             current_position += 1
         previous.next = node
         node.next = current
     return self.head
예제 #3
0
    def enqueue(self, data):
        new_item = Node(data)

        current = self.head
        if current is None:
            self.head = new_item
        else:
            while current.getNext():
                current = current.getNext()
            current.setNext(new_item)
예제 #4
0
 def add(self, item):
     temp = Node(item)
     temp.setNext(self.head)
     self.head = temp