#include<stdio.h> #include<stdlib.h> typedef struct Node{ int data; struct Node* next; }Node; Node* AddAtEnd(Node** head,int data){ Node* new_node = (Node*)malloc(sizeof(Node)); new_node->data = data; new_node->next = NULL; if(*head == NULL) { *head = new_node; return new_node; } Node *cur = *head; while(cur->next!=NULL) { cur = cur->next; } cur->next = new_node; return new_node; } Node* deleteNode(Node **head){ Node* cur = *head; if(cur==cur->next->next) //只剩两个数的情况 { cur->next = NULL; return cur; } else //剩三个数以上 { Node *temp = cur->next->next; cur->next = cur->next->next; return temp; } } int main(){ int n; scanf("%d",&n); Node* head = NULL; Node* node = NULL; for (int i = 0; i < n; i++) { node = AddAtEnd(&head,i+1); } node->next = head; Node* now = head; while(now->next!=NULL) { now = now->next; now = deleteNode(&now); //printf("%d\n",now->data); } printf("%d\n",now->data); return 0; }
0.0分
0 人评分
C语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C语言代码)浏览:636 |
C语言程序设计教程(第三版)课后习题6.2 (C语言代码)浏览:751 |
C语言程序设计教程(第三版)课后习题6.6 (C语言代码)浏览:366 |
完数 (C语言代码)浏览:757 |
用筛法求之N内的素数。 (C语言代码)浏览:711 |
图形输出 (C语言代码)浏览:1422 |
Tom数 (C语言代码)浏览:581 |
简单的a+b (C语言代码)浏览:683 |
C二级辅导-等差数列 (C语言代码)浏览:891 |
数列有序 (C语言代码)浏览:974 |