#include <stdio.h> typedef struct Node { int value; struct Node* next; } Node; //按升序插入 void insert(Node* head, int newValue) { Node* newNode, *p, *pre; //创建新节点 newNode = (Node*)malloc(sizeof(Node)); newNode->value = newValue; newNode->next = NULL; //头插法 if (head->next==NULL) { head->next = newNode; } else { pre = head; p = head->next; while (p != NULL && newValue > p->value) { pre = pre->next; p = p->next; } newNode->next = p; pre->next = newNode; } } int main() { int n; while (scanf("%d ", &n) != EOF) { // 注意 while 处理多个 case //带头节点 Node* head = (Node*)malloc(sizeof(Node)),*p; head->value = 0; head->next = NULL; int newValue; for (int i = 0; i < n; i++) { scanf("%d ", &newValue); insert(head, newValue); } //输出链表节点元素 p = head->next; while (p != NULL) { printf("%d ", p->value); p = p->next; } printf("\n"); } return 0; }