Joseph Ring
Joseph Ring operation as follows: 1, a group of people sitting together into a ring (such as: number N) 2, starting from a number (such as: K default 1) 3, count to a certain number (such as: M), the person out, the next person re-counted 4, has been circulating until everyone out, Joseph Ring end.
#include <stdio.h>#include<malloc.h>
Linked list node structurestructnode{structNode *Next; intdata;};
Create a head node, and each time a new node is hooked up to the head node, the last node points to the next node of the head junction. Finally, the next node of the last node, the new head junction, is returned.
Circular Link List node*create (intnum) {Node*head=NULL; Head= (node *)malloc(sizeof(node)); Node*p=Head; Node*s; intI=1; while(i<=num) {s= (node*)malloc(sizeof(node)); S->data=i++; S->next=NULL; P->next=s; P=s; } s->next=head->Next; Free(head); returns->next;//1}intMain () {intnum=Ten;//Total 10 people intm=3;//number 3rd, 1 2 3|, 3 outs .node*current=Create (NUM); Node*temp; while(Current! = current->next) { //move back two bits for(intI=1; i<m-1; i++) { current=current->Next; } temp=current->Next; Current->next=temp->Next; printf ("%d->",temp->data); Free(temp); Current=current->Next; } printf ("\ n"); printf ("The last is %d",current->data); return 0;}
Results:
C-Joseph Ring