#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
int data;//值
struct node* next;//嵌套定义结构体必须有名
}linklist;
int main()
{
//输入数字创建循环链表
linklist* head;
linklist* s;
linklist* r;
int n = 0;
int i = 1;
head = (linklist*)malloc(sizeof(linklist));//生成头结点
r = head;//尾指针初值指向头结点
scanf("%d", &n);
for (i = 1; i < n + 1; i++)
{
s = (linklist*)malloc(sizeof(linklist));//生成一个新结点
s->data = i;
r->next = s;//新结点插入表尾
r = s;//尾指针指向新表尾
}
r->next = head->next;
//计数输出删除
linklist* p;
p = head->next;
int j = 0;
int k = 0;
scanf("%d", &k);
while (p != NULL)
{
for (j = 1; j < k; j++)
{
p = p->next;
}
printf("%d ", p->data);
linklist* q;
q = p->next;
p->data = q->data;
p->next = q->next;
if (p != q)
{
free(q);
}
else
{
p = NULL;
}
}
}
标签:head,Josephus,int,作业,结点,next,linklist,data
From: https://blog.51cto.com/u_15736615/7850527