C-Joseph Ring

Source: Internet
Author: User

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

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.