利用环形链表实现 Josephus杀人游戏 1 #include <stdlib.h> 2 #include <stdio.h> 3 #include "circular.h" 4 5 void make_n(int n); 6 void kill_one(int, int); 7 void main(int argc, char *args[]){ 8 int i=1; //用i来记录当前点数 9 int n,m; 10 scanf("%d", &n); 11 scanf("%d", &m); 12 make_n(n); 13 printf("n=%d, m=%d\n",n,m); 14 list_node(); 15 printf("------------------\n"); 16 kill_one(m, i); 17 18 } 19 20 21 void make_n(int n) 22 { 23 link node = malloc(sizeof *node); 24 for(; n!=0; n--){ 25 node = make_node(n); 26 insert(node); 27 } 28 } 29 30 31 void kill_one(int m, int i) 32 { 33 link head = get_head(); 34 link node = head->next; 35 int len = 0; 36 while(node != head){ 37 if(i==m){ 38 printf("=========ko------------------=%d\n", node->item); 39 delete(node); 40 i=0; 41 } 42 i++; 43 node = node->next; 44 } 45 list_node(); 46 head = get_head(); 47 for(node=head; node->next != head; node=node->next){ 48 len++; 49 } 50 if(len>=m){ 51 printf("------------------\n"); 52 kill_one(m, i); 53 }else{ 54 printf("head->next->item=%d\n", head->next->item); 55 } 56 } 57