Loop linked list, the final tail pointer pointing to the head knot point, two-way loop linked list is to add a pre pointer.
Questions about Joseph's ring: set N people numbered 1, 2, 3,... n to sit in a circle. The person whose sequence number is K (1 <= k <= N) starts counting from 1. The person whose number is m is listed, and his next digit starts counting from 1, those who count to m are listed again, and so on until all people are listed.
For example, if n = 8, K = 3, M = 4, the column sequence is: 6 2 7 4 3 5 1 8.
Algorithm idea: Use a circular linked list with no leading nodes to deal with the problem of Joseph Phu: first form a single circular linked list with N nodes, and then count from 1 on the K node, when M is recorded, the corresponding node is deleted from the linked list, and then counted from 1 starting from the next node of the deleted node ....., the algorithm ends until all nodes are listed.
#include <stdio.h>#include <errno.h>#include <stdlib.h>#define N 10typedef int datatype;typedef struct node { datatype data; struct node * next;}linknode ,*linklist;linklist create(){ linklist H; if((H = (linklist)malloc(sizeof(linknode)))==NULL) { perror("malloc"); exit(-1); } H->next = H; H->data = 1; printf("create\n"); return H;}int insert(linklist H,datatype data , int pos){ int i; linklist q; linklist p = NULL; p = H; if((pos<0)) { printf("insert error\n"); return -1; } if((q = (linklist)malloc(sizeof(linknode))) == NULL ) { perror("malloc"); exit(-1); } while(pos--) p=p->next; q->data = data; q->next = p->next; p->next = q; return 0;}int show(linklist H,int k,int m){ int i = k-2; linklist p,q; p = H; while(i--) p = p->next; while(p != p->next) { for(i = 0;i < m-1; i++) p = p->next; q = p; p = p->next; q ->next = p->next; printf("data =%d\n",p->data); free(p); p = q->next; } printf("data =%d\n",p->data); return 0;}int main(int argc,char * argv[]){ int n,m,k,i = 0; int ret; linklist H; H = create(); scanf("%d",&n); scanf("%d",&m); scanf("%d",&k); for(i = 2;i < n+1;i++) { ret = insert(H,i,i-2); if(ret ==-1) printf("insert error\n"); } printf("**********create linklist*************\n"); show(H,m,k); return 0;}