约瑟夫环
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}Node,*list;
int main(int argc, const char *argv[])
{
list head = (list)malloc(sizeof(Node));
head->data = 0;
head->next = NULL;
list tail = head;
for(int j=0;j<8;j++)
{
list p = (list)malloc(sizeof(Node));
p->data = j+1;
tail->next = p;
p->next = head->next;
tail = p;
}
list p = head->next;
list q = tail;
int h =1;
while(p!=q)
{
if(h==4)
{
printf("%d\t",q->next->data);
q->next=q->next->next;
free(p);
p=q->next;
h=1;
}
else
{
q=p;
p=p->next;
h++;
}
}
putchar(10);
head->next=q;
free(p);
head->next=NULL;
return 0;
}