新人解决老问题,约瑟夫链表问题,指点下吧
程序代码:
#include <stdio.h> #include <stdlib.h> struct node{ int password; int array; int exit; struct node *next; }; #define INFEASIBLE -2; #define OK 0; typedef struct node linklist; int ysf(linklist *h,int m,int n){ linklist *q=h; int i,count,ps; i=0; ps=m; while(i<n){ for(count=0;count<ps;) if(q->exit) count++; printf("%d",q->array); ps=q->password; q->exit=0; } return OK; } int creat(linklist *h,int n){ int i; linklist *p; if(h==NULL) return INFEASIBLE; for(i=1;i<n;i++){ p=(linklist*)malloc(sizeof(struct node)); p->next=h->next; p=h->next; } i=0; printf("Please input the password:\n"); scanf("%d",&h->password); h->array=i++; h->exit=1; for(p=h->next;p!=h;p=p->next){ scanf("%d",&p->password); p->array=i++; p->exit=1; } return OK; } int main(){ linklist *h; int M,N; printf("Please input the M and the N:"); scanf("%d%d",&N,&M); h=(linklist*)malloc(sizeof(struct node)); creat(h,M); ysf(h,N,M); return 0; }
编译通过了,求给看看