传说中的报数问题
传说中的报数问题程序代码:
#include<stdio.h> #include<stdlib.h> struct alist{ int data; struct alist *next; }; typedef struct alist list; list *head,*move,*work; list *setup(int); int delte_list(list *,int,int); int print_out_data(int *); int i,j,k=0,start,end,*out=(int *)calloc(100,sizeof(int)); int main(){ list *newlist; printf("您需要几个人来进行这个游戏\n"); scanf("%d",&i); newlist=setup(i); printf("您需要编号为几的人开始游戏\n"); scanf("%d",&start); printf("您需要数到几的人出列\n"); scanf("%d",&end); delte_list(newlist,start,end); print_out_data(out); } list *setup(int i){ head=move=(list *)malloc(sizeof(list)); head->data=i; for(j=0;j<i;j++){ work=(list *)malloc(sizeof(list)); work->data=j+1; move->next=work; move=work; } move->next=head->next; return head; } int delte_list(list *newlist,int start,int end){ list *p; if(start>=head->data||end>head->data-start){ printf("输入有误请确认\n"); exit(0); }else{ move=head; /*头结点数赋值给了move*/ work=move->next; /*第1个数赋值给了work*/ for(j=1;j<start;j++){ move=move->next; work=work->next; } while(head->data>=end){ for(j=1;j<end;j++){ move=move->next; work=work->next; } p=work; out[k]=work->data; head->data--; k++; move->next=work->next; work=p->next; free(p); } out[k]='\0'; } return *out; } int print_out_data(int *out){ printf("依次出列的编号为"); i=0; while(out[i]){ printf("%d,",out[i]); i++; } printf("\n"); return 0; }