#include<stdio.h> #include<string.h> #include<malloc.h> typedef struct Node{ int c; struct Node* next; }NODE, * node; typedef struct{ char* head; int len; }LIST, * list; int main(){ int n; int status = scanf("%d", &n); LIST l; l.head = NULL; l.len = n; node t = NULL; node tail = NULL; for(int i = 1; i <= n; i++){ t = (node)malloc(sizeof(NODE)); if(t != NULL){ t->c = i; t->next = NULL; if(tail != NULL){ tail->next = t; tail = tail->next; //printf("第%d次: %p\n", i,l.tail); } else{ tail = t; l.head = t; //printf("仅仅一次: %p\n", l.tail); } } } int j = 0; node temp = NULL; node temp_0 = NULL; while(l.len != 1){ for(t = l.head; t;){ j++; if(j % 3 == 0){ if(t == l.head){ l.head = t->next; } temp_0 = t;//当前要删除的指针. t = t->next;//转到下一个指针. if(temp != NULL) temp->next = t;//把上一个指针链接到下一个指针 free(temp_0);//当前指针被删除. temp_0 = NULL; j = 0; l.len--; if(l.len == 1) break; } else{ temp = t;//上一个指针 t = t->next;//下一次循环,因为j不满足%3==0,所以要跳到下一个. } } } temp = l.head; printf("%d", temp->c); free(temp); temp = NULL; tail = NULL; t = NULL; return 0; }
0.0分
0 人评分