Demand expression: slightly
Analysis:
Realize:
#include <stdio.h>#include<stdlib.h>typedefstructNode {intpayload; structnode*Next;} node;/*Function: Inserts a node at the tail of the Joseph Ring. Add * param:node* tail Joseph Ring's Tail knot; * return:node* tail return to the new Joseph ring tail Knot **/node* Add (node*tail) { if(Tail = =NULL) {Tail= (node*)malloc(sizeof(node)); TailNext =tail; returntail; } Else{node* New_tail = (node*)malloc(sizeof(node)); New_tailNext = TailNext; TailNext =New_tail; returnNew_tail; }}/*Function: Traverse Joseph Ring, traverse_joseph_circle *param:node* tail *return:void * **/voidTraverse_joseph_circle (node*tail) {Node* move = tail;//Pointer to move//Overall idea: A little knot in the case, into the loop, the tail node and the relationship between the head node to kill, show a chain, back to the head node, move down, before moving down, first traveled this node, in judging can be downtravels. while(Move! =NULL) {Move= Move, Next;//move back to the node.printf"%d;", Move-payload); if(move = = tail) Break ; } printf ("\ n");}/*Function: The implementation of the Joseph ring problem. Eliminate *param:node* tail; int step *return:void * **/voidEliminate (node* tail,intStep) {Node* move =tail; Node* Save_previous =tail; intCount =0 ; while(NULL! =tail) {save_previous=move; Move= Move-Next; if(move = = Move->next) Break; if(+ + count = =Step) {save_previousNext = MoveNext; printf ("Current Delete node:%d\n", Move-payload); if(tail = = move) tail = save_previous;//Update Joseph Ring Free(move); printf ("current node: \ n"); Traverse_joseph_circle (tail); Move=save_previous; Count=0 ; } }}intMain () {node*tail; //construct Joseph Ring of ten nodes inti; for(i =0; I < -; i + +) {Tail=Add (tail); Tail-Payload =i; } traverse_joseph_circle (tail); Eliminate (tail,3);}
Effect:
[Email protected] joseph_circle]$.Joseph_circle.out0;1;2;3;4;5;6;7;8;9to delete a node at the current point:2Current node:0;1;3;4;5;6;7;8;9to delete a node at the current point:5Current node:0;1;3;4;6;7;8;9to delete a node at the current point:8Current node:0;1;3;4;6;7;9to delete a node at the current point:1Current node:0;3;4;6;7;9to delete a node at the current point:6Current node:0;3;4;7;9to delete a node at the current point:0Current node:3;4;7;9to delete a node at the current point:7Current node:3;4;9to delete a node at the current point:4Current node:3;9to delete a node at the current point:9Current node:3;
C-Language linked list implementation Joseph ring problem