5 条题解
-
1
C语言单向链表插排
#include <stdlib.h> #include <stdio.h> typedef struct _List { int num; struct _List *ne; } List; //节点结构体 int n; List *head = NULL, *tail = NULL; void init(int x) { List *temp = (List*)malloc(sizeof(List)); temp -> num = x; temp -> ne = NULL; head = tail = temp; } //链表初始化(第一个节点) void insert_to_head(int x) { List *temp = (List*)malloc(sizeof(List)); temp -> num = x; temp -> ne = head; head = temp; } //头部插入 void insert_to_tail(int x) { List *temp = (List*)malloc(sizeof(List)); temp -> num = x; temp -> ne = NULL; tail -> ne = temp; tail = temp; } //尾部插入 void insert(int x, List *y) { List *temp = (List*)malloc(sizeof(List)); temp -> num = x; temp -> ne = y -> ne; y -> ne = temp; } //在y节点后插入 int main() { scanf("%d", &n); for(int i = 1, x; i <= n; i++) { scanf("%d", &x); if(i == 1) init(x); else { List *pr = NULL, *temp; for(temp = head; temp != NULL; temp = temp -> ne) { if(temp -> num > x) { if(pr == NULL) insert_to_head(x); else insert(x, pr); break; } pr = temp; } if(temp == NULL) insert_to_tail(x); } } for(List *temp = head; temp != NULL; temp = temp -> ne) { printf("%d\n", temp -> num); } return 0; }
- 1
信息
- ID
- 83
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 4
- 标签
- 递交数
- 605
- 已通过
- 302
- 上传者