Joseph's problem is a famous one: N people are in a circle, starting from the first to report the number, M will be killed, and the last one will be killed. For example, N = 6, M = 5, and the number of the person killed is 5, 4, 6, 2, 3. The last part is the 1st.
# Include <stdio. h> # include <stdlib. h> # include "declear. h "typedef struct Node {ElemType data; struct Node * next;} Node, * cylinklist; void Joseph PHUs (int totalNum, int startNum, int interval) {int index; cylinklist node; cylinklist temPtr; cylinklist temPriorPtr;/* Create a circular linked list with no headers */cylinklist L = (cylinklist) malloc (sizeof (Node); L-> data = 1; l-> next = L; temPtr = L; for (index = 2; index <= TotalNum; index ++) {node = (cylinklist) malloc (sizeof (Node); node-> data = index; temPtr-> next = node; node-> next = L; temPtr = node;} temPtr = L;/* locate the start position */for (index = 1; index <startNum; index ++) {temPriorPtr = temPtr; temPtr = temPtr-> next;} while (temPtr! = TemPtr-> next) {/* locate the position where the number of reports is interval from the start position */for (index = 1; index <interval; index ++) {temPriorPtr = temPtr; temPtr = temPtr-> next;} printf ("% d \ n", temPtr-> data ); /* Delete the vertex from the linked list */temPriorPtr-> next = temPtr-> next; free (temPtr ); /* Update start position */temPtr = temPriorPtr-> next ;}} int main () {Joseph PHUs (9, 2, 4); return 0 ;}