#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语言代码)浏览:435 |
哥德巴赫曾猜测 (C语言代码)浏览:1018 |
人见人爱A+B (C语言代码)浏览:628 |
C语言程序设计教程(第三版)课后习题8.6 (C语言代码)浏览:600 |
C语言训练-求函数值 (C语言代码)浏览:580 |
WU-字符串比较 (C++代码)浏览:757 |
简单的a+b (C语言代码)浏览:528 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:702 |
C语言考试练习题_一元二次方程 (C语言代码)浏览:576 |
1011题解浏览:765 |