/*
链表实现方式
*/
#include<stdio.h>
#include<stdlib.h>
typedef struct people
{
int card;
struct people *next;
};
void main()
{
int i;
int sum = 1;
struct people *p,*q,*s,*m;//p表示循环输入的那个变量,q表示开始节点。
p = q = (struct people *)malloc(sizeof(struct people));
p->card = 1;
p->next = (struct people *)malloc(sizeof(struct people));
p = p->next;
for(i = 0;i < 49;i++)
{
p->card = i+2;
if(i != 48)
{
p->next = (struct people *)malloc(sizeof(struct people));
p = p->next;
}
else
p->next = q;
}
p = q;//p重新指向开始结点
while(p != p->next)
{
if(sum == 3)
{
m = p;
p = p->next;
s->next = p;
sum++;
free(m);
m = NULL;
}
else if(sum==7)
{
m = p;
p = p->next;
s->next = p;
sum = 1;
free(m);
m = NULL;
}
else
{
s = p;
p = p->next;
sum++;
}
}
printf("50名队员围成一圈,顺序排号,从第一个开始报数(从1到7报数),凡报到3和7的人退出圈子,报到7后,剩下的人再继续从1到7报数,最后留下的是原来第%d号队员\n",p->card);
free(p);
p = NULL;
}