/* An example of simulation using circular queue */ #include #include typedef struct node { int player_id; struct node *next; } node; int main(int argc, char *args[]) { //setbuf(stdout, NULL); //game parameters, n -- number of players, k -- remove the kth player int n=10, k =2; if (argc >1) n = atoi(args[1]); if (argc >2) k = atoi(args[2]); // Create circular linked list containing all the players node *start, *ptr, *new_node, *temp; start = malloc(sizeof(struct node)); start->player_id = 1; ptr = start; int i; for (i = 2; i <= n; i++) { new_node = malloc(sizeof(struct node)); new_node->player_id = i; ptr->next = new_node; ptr = new_node; } ptr->next = start; //execution, start from 1, remove the kth position by circular count int count; for (count = n; count > 1; count--) { for (i = 0; i < k - 1; ++i) { ptr = ptr->next; } temp = ptr->next; ptr->next = ptr->next->next; // Remove the eliminated player from the circular linked list free(temp); } printf("\nThe Winner is Player %d", ptr->player_id); return 0; }