-
个人简介
import random
def generate_data(): """生成n个0~100的随机整数链表""" n = 10 nodes = [] head = -1 for i in range(n): nodes.append([random.randint(0, 100), -1]) if i == 0: head = i else: nodes[i-1][1] = i return head, nodes
def print_list(head, nodes): """打印链表中的所有值(顺序)""" if head == -1: print("空链表") return cur = head vals = [] while cur != -1: vals.append(str(nodes[cur][0])) cur = nodes[cur][1] print(" -> ".join(vals))
def louis_sixteen_sort(head, nodes): """路易十六排序(降序)""" if head == -1 or nodes[head][1] == -1: return head
return head生成初始数据
head, nodes = generate_data() print("原始链表(随机顺序):") print_list(head, nodes)
执行排序
sorted_head = louis_sixteen_sort(head, nodes) print("\n排序后链表(降序):") print_list(sorted_head, nodes)
-
最近活动
This person is lazy and didn't join any contests or homework.