Skip to content

Instantly share code, notes, and snippets.

@flyfire
Created October 6, 2013 14:00
Show Gist options
  • Select an option

  • Save flyfire/6854459 to your computer and use it in GitHub Desktop.

Select an option

Save flyfire/6854459 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
typedef struct link_tag
{
int no;
struct link_tag *next;
}Link;
void createJoseph(Link *&L, int n)
{
L = NULL;
int i;
Link *s,*r;
for (i = 0; i < n; i++)
{
s = (Link *)malloc(sizeof(Link));
s->no = i+1;
if (NULL == L)
{
L = s;
s->next = L;
r = s;
}
else
{
r->next = s;
s->next = L;
r = s;
}
}
}
void dispJoseph(Link *L)
{
if (NULL != L)
{
Link *s = L;
while (s->next != L)
{
printf("%4d",s->no);
s = s->next;
}
printf("%4d\n",s->no);
}
}
void showDemo(Link *&L, int m)
{
Link *r = L, *q;
while (r->next != r)
{
for (int i = 1; i < m-1; i++)
{
r = r->next;
}
printf("%4d",r->next->no);
q = r->next;
r->next = q->next;
free(q);
r = r->next;
}
printf("%4d\n",r->no);
free(r);
}
int main(int argc, char **argv)
{
int n,m;
printf("有多少个人?");
scanf("%d",&n);
scanf("%*[^\n]");
Link *JosephCircle;
createJoseph(JosephCircle,n);
dispJoseph(JosephCircle);
printf("第几个人出列?");
scanf("%d",&m);
scanf("%*[^\n]");
printf("出列顺序是:\n");
showDemo(JosephCircle,m);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment