利用单循环连表。
#include
#include
typedef struct node
{ int data;
struct node* link;
}humannode;
#define N 20 ///定义20个人
void jesephu(int m)
{
humannode *plist=NULL,*list=NULL;
humannode *newnode=NULL;
humannode * del=NULL; //删除节点
plist=(humannode*)malloc(sizeof(humannode));
list=plist;
plist->data=1;
plist->link=plist;
int n=0;
int count=1;
for(int i=2;i<=N ;i++) //初始化循环连表
{
newnode=(humannode*)malloc(sizeof(humannode));
if(newnode==NULL)return ;
newnode->data=i;
list->link=newnode;
list=list->link;
}
list->link=plist;//头节点为plist 构成循环
while(n { while(count { plist=plist->link;++count;
}
del=plist->link;
printf("%d out\n",del->data);
plist->link=del->link;
plist=plist->link;
free(del);
n++;
count=1;
}
}
int main()
{
jesephu(3);
// system("pause");
return 0;
}
阅读(1507) | 评论(1) | 转发(0) |