10 条题解
-
2
C语言单向循环链表写法
#include <stdlib.h> #include <stdio.h> typedef struct _List { int num; struct _List *ne; } List; //节点结构体 int n, m; List *head, *tail; //头指针、尾指针 void init(int x) { List *temp = (List *)malloc(sizeof(List)); temp -> num = x; temp -> ne = NULL; head = tail = temp; } //初始化链表(第一个节点) void insert_to_tail(int x) { List *temp = (List *)malloc(sizeof(List)); temp -> num = x; temp -> ne = NULL; tail -> ne = temp; tail = temp; } //将节点插入到尾部 int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) { if(i == 1) init(i); else insert_to_tail(i); } //建立链表 tail -> ne = head; //首尾相连(循环链表) int rec = 0; for(List *temp = head, *pr = NULL; temp != temp -> ne;) { //遍历循环链表 if(++rec % m == 0) { //踢人 printf("%d ", temp -> num); pr -> ne = temp -> ne; free(temp); head = temp = pr -> ne; } else { pr = temp; head = temp = temp -> ne; } } printf("\n%d\n", head -> num); return 0; }
信息
- ID
- 105
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 7
- 标签
- 递交数
- 1460
- 已通过
- 288
- 上传者