Add a node in ascending order

``````def add(self, value):

new_node = Node(value)

new_node = Node(value)
new_node.value = value

else:
new_node = Node(value)
self.tail.setNext(new_node)
self.tail = new_node

self.count += 1
``````

``````>>> x.add(8)
>>> print(x)
List:-88 -6 1 3 7 8 33 58
``````

``````>>> print(x)
Tail:Node(1)
List:-88 -6 3 7 8 58 33 1
``````

1 个回复

• 8->如果是第一个-> 8
• 7-> elif 8> 7-> 7,8
• 3，-6->省略号相同-> -6，3，7，8
• 58->最后---6、3、7、8、58

``````def add(self, value):

#if list is empty
new_node = Node(value)

new_node = Node(value)
new_node.value = value

#elif value > tail set new tail
elif value > self.tail.value:
new_node = Node(value)
self.tail.setNext(new_node)
self.tail = new_node
# and finally you need to loop to find the sweet spot
else:
# we will start the search from the head
# while the value we wish to insert is bigger than the next one
while value > current_node.next.value:
# set the current one to the next one
current_node = current_node.next
# finally we reached a node which is smaller than the value we wish to insert
# but its next node is bigger
new_node = Node(value)
# set the new nodes next to the bigger node
new_node.next = current_node.next
# and the curren't node's next to the new one
current_node.next = new_node
``````
1 升序插入节点

2017-11-09 18:01:58 2 50   c
2 在适当的位置添加一个节点（列表将按升序排列）

3 在Datagridview中按升序添加行

4 将节点按升序插入链表

5 将元素按升序添加到排序的数组中

2015-04-02 08:45:01 2 815   python
7 是否升序

2014-10-19 19:41:11 4 111   java
8 订单升序了吗？

9 订单数组升序

10 按升序排列列

2019-11-10 04:11:11 2 122   csv