我在弄清楚如何在Python中对单链接列表进行排序时遇到了一些麻烦.我已经想出了如何创建链接列表并将数据推送到它上但是如何以排序的格式推送它(在所有数据被推到它之后不进行排序)或者只是以任何方式对其进行排序?
根据用户输入创建排序的单链数字列表.程序逻辑:询问一个数字,将该数字添加到排序位置的列表中,打印列表.重复,直到他们为数字输入-1.
#!/usr/bin/env python
class node:
def __init__(self):
self.data = None # contains the data
self.next = None # contains the reference to the next node
class linked_list:
def __init__(self):
self.cur_node = None
def add_node(self, data):
new_node = node() # create a new node
new_node.data = data
new_node.next = self.cur_node # link the new node to the 'previous' node.
self.cur_node = new_node # set the current node to the new one.
def list_print(self):
node = self.cur_node # cant …Run Code Online (Sandbox Code Playgroud)