文章
20
粉丝
224
获赞
56
访问
137.4k
#include<stdio.h>
#include<malloc.h>
struct node{
int num;
struct node *next;
} node;
int N;
// 创建循环链表
struct node *create() {
struct node *head, *now, *pre;
for (int i = 1; i <= N; ++i) {
now = (struct node *) malloc (sizeof(node));
if (i == 1) {
head = now;
pre = now;
}
now->num = i;
now->next = head;
pre->next = now;
pre = now;
}
return head;
}
// 按照题目要求输出
void print(struct node *head) {
struct node *p, *pre; // pre 是前一个节点
p = head;
int i = 1;
while (p != NULL) {
if (p == p->next) { // 只剩最后一个
printf("%d", p->num);
break;
}
if (i % 3 == 0) pre->next = p->next; // 删除它
else pre = p; // 这里一定要用 else 如果是删除的话 pre 不变
p = p->next;
++i;
}
}
int main(){
struct node *head;
scanf("%d", &N);
head = create();
print(head);
}
登录后发布评论
暂无评论,来抢沙发