示例#1
0
 def insert(self, value):
     """Insert values into a linked list."""
     node = Node(value)
     node._next = self.head
     self.head = node
     # this line is the same as the three above it.
     # self.head - Node(value, self.head)
     self.size += 1
 def add_after(self, input, target):
     """Add a new node after a target node."""
     current = self.head
     new_node = Node(input)
     if target == self.head.value:
         new_node._next = self.head._next
         self.head._next = new_node
         self.length += 1
     else:
         while current:
             if current.value != target:
                 current = current._next
             elif current.value == target:
                 new_node._next = current._next
                 current._next = new_node
                 self.length += 1
                 return
示例#3
0
    def insert(self, val):
        '''
        '''
        node = Node(val)  # Create new node in object
        node._next = self.head  # Assigns next value to previous node.
        self.head = node  # Assign head to new node
        # self.head = Node(val, self.head)

        self._length += 1  # tracks the lenght
        return self.head.val  # returns newly created node.
 def add_before(self, input, target):
     """Add a node before a existing node in the LinkedList."""
     current = self.head
     previous = None
     new_node = Node(input)
     if target == self.head.value:
         new_node._next = self.head
         self.head = new_node
         self.length += 1
         return
     while current:
         if current.value != target:
             previous = current
             current = current._next
         elif current.value == target:
             new_node._next = current
             previous._next = new_node
             self.length += 1
             return
 def add(self, input):
     """Add a node to the front of the Linked List."""
     new_node = Node(input)
     if self.head is None:
         self.head = new_node
         self.length += 1
     else:
         new_node._next = self.head
         self.head = new_node
         self.length += 1
示例#6
0
 def push(self, val):
     """
     places a new node on top of the stack and sets it as the new top
     """
     node = Node(val)
     node._next = self.top
     self.top = node
     # self.top = Node(val, self.top) <- is dryer way to do this
     self._size += 1
     return self.top
示例#7
0
 def add(self, value):
     """Add a node to the stack."""
     new_node = Node(value)
     if self.top is None:
         self.top = new_node
         self.height += 1
     else:
         temp = self.top
         self.top = new_node
         new_node._next = temp
         self.height += 1
示例#8
0
    def push(self, val):
        """ What is this"""

        try:
            node = Node(val)
        except TypeError:
            #handle the thing
            pass

        node._next = self.top
        self.top = node

        return self.top
示例#9
0
    def push(self, val):
        """Doc String"""
        # if type(val) is not int:
        #     raise TypeError('Argument for val must be an Int.')

        try:
            node = Node(val)
        except TypeError:
            # handle the thing
            pass

        node._next = self.top
        self.top = node

        return self.top
示例#10
0
 def insert_before(self, val, newVal):
     current = self.head
     previous = None
     while current:
         if current.val == val:
             if previous is None:
                 self.insert(newVal)
             else:
                 new_node = Node(newVal)
                 new_node._next = current
                 previous._next = new_node
                 self._size += 1
             break
         previous = current
         current = current._next
示例#11
0
 def insertBefore(self, value, newVal):
     """Insert before a given value in a linked list."""
     current_node = self.head
     previous_node = None
     while current_node:
         if current_node.val == value:
             if previous_node is None:
                 self.insert(newVal)
             else:
                 new_node = Node(newVal)
                 new_node._next = current_node
                 previous_node._next = new_node
                 self.size += 1
             break
         previous_node = current_node
         current_node = current_node._next
    def append(self, val):
        """add node to end of linked list"""
        if self._size == 0:
            self.insert(val)
            return
        node_index = self.head
        if not node_index._next:
            node_index = Node(val)

        while node_index._next:
            node_index = node_index._next
            print('while loop')
            print('node index => {}'.format(node_index))
        node_index._next = Node(val)
        self._size += 1
        return 'length => {}'.format(self.__len__())
 def insert(self, index, value):
     if index <= 1:
         self.addFirst(value)
     elif index > self._length:
         self.append(value)
     else:
         newNode = Node(value)
         count = 1
         pre = None
         current = self._head
         while count < index:
             pre = current
             current = current._next
             count += 1
         pre._next = newNode
         newNode._next = current
     self._length += 1
 def insert_before(self, val, newVal):
     """
     Add a new node with the given newValue immediately before the first value node
     """
     current = self.head
     previous = None
     while current:
         if current.val == val:
             if previous is None:
                 self.insert(newVal)
             else:
                 new_node = Node(newVal)
                 new_node._next = current
                 previous._next = new_node
                 self._size += 1
             break
         previous = current
         current = current._next
示例#15
0
 def insert_before(self, val, newVal):
     """Insert a new node before the matching value."""
     search = self.head
     previous = None
     if type(val) is not int:
         raise Exception('Please enter an integer.')
     try:
         while search:
             if search.val == val:
                 if previous is None:
                     self.insert(newVal)
                 else:
                     new_node = Node(newVal)
                     new_node._next = search
                     previous._next = new_node
                     self._size += 1
             previous = search
             search = search._next
     except (KeyError, ValueError):
         raise Exception('Value not found!')
示例#16
0
 def insert(self, val):
     node = Node(val)
     node._next = self.head
     self.head = node
     self._size += 1
 def addFirst(self, value):
     newNode = Node(value)
     newNode._next = self._head
     self._head = newNode