文章
9
粉丝
289
获赞
4
访问
81.8k
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
//创建一个循环链表(不带头结点)
node* createCirList(int num[], int length)
{
node *head, *p, *r;
head = (node *)malloc(sizeof(node));
head->data = num[0];
head->next = head;//自己指向自己,构建循环链表
r = head; //r尾指针
for(int i=1; i<length; i++)
{
p = (node *) malloc(sizeof(node));
p->data = num[i];
p->next = head;
r->next = p;
r = p;
}
return head;
}
int main()
{
//输入
int N;
scanf("%d", &N);
int num[N];
for(int i=0; i<N; i++)
{
num[i] = i+1;
}
//创建循环链表
node *L = createCirList(num, N);
//传花
node *p = L;
while(p->next!=p)//当剩下不止一个结点
{
node *pre = p->next;
p = p->next->next;
//删除p指向的结点
pre->next = p->next;
p ...
登录后发布评论
暂无评论,来抢沙发