C-Language linked list implementation Joseph ring problem

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.