代码如下:
//约瑟夫环函数
void josepho(Looplink *L,int k){
if(NULL == L){
puts("所给链表不合法");
return;
}
Looplink *q = L;
Looplink *p;
int n = L->len;
for(int i = 0;inext;
if(q == L){
j--;
}
}
p = q->next;
if(p == L){
p = p->next;
}
printf("%d\t",p->data);
q->next = p->next;
free(p);
L->len--;
}
puts("");
}
main.c
#include
#include
#include
#include "homework.h"
int main(int argc, const char *argv[])
{
Looplink *L = list_create();
if(NULL == L){
return -1;
}
list_insert_head(L,7);
list_insert_head(L,6);
list_insert_head(L,9);
list_insert_head(L,5);
list_insert_head(L,8);
list_insert_head(L,2);
list_insert_head(L,3);
list_insert_head(L,1);
list_insert_head(L,4);
list_show(L);
josepho(L,4);
return 0;
}
终端执行结果:
ubuntu@ubuntu:2homework$ gcc *.c
ubuntu@ubuntu:2homework$ ./a.out
创建成功
插入成功
插入成功
插入成功
插入成功
插入成功
插入成功
插入成功
插入成功
插入成功
4 1 3 2 8 5 9 6 7
2 6 3 7 5 8 9 1 4