C语言对约瑟夫链表的简单实现代码
#include<stdio.h>
#include<malloc.h>
typedef struct xhlb
{
int data;
struct xhlb *next;
}list;
list *creat_list(int n)
{
list *p,*q,*head;
int i=1;
head=NULL;
p=q=head;
while(i<n+1)
{
p=(list *)malloc(sizeof(list));
p->data=i;
i++;
if(head==NULL)
head=p;
else
q->next=p;
q=p;
}
q->next=head;
return head;
}
int main()
{
list *p,*q;
int n,m,j=1;
printf("n=");
scanf("%d",&n);
printf("nm=");
scanf("%d",&m);
printf("n");
p=creat_list(n);
q=p->next;
while(p!=p->next)
{
if(j==(m-1))
{
printf("%4d",q->data);
q=q->next;
p->next=q;
j=0;
}
p=q;
q=q->next;
j++;
}
printf("%4dn",p->data);
return 0;
}
