"Turn" Joseph Ring algorithm---------topic: There are n individuals around in a circle, order automatic arranging, from the first start off (from 1 to 3 off), where 3 of the people quit the circle, ask the last left is the original number of the first.

Source: Internet
Author: User

Tips : implemented with ring listFor this topic is actually uses the C language circular link list realization one Joseph Ring. We can define a circular linked list, add this n person to the list, and then define three node pointers on the list of loops, move the span of 3, using the loop function of the list to delete the third node each time, one of the problems here is that you define 3 pointers, and in the loop they are each other also has Next relationship, generally we determine whether the loop end condition is the next node of a node is it itself (such as Ptr->next = = ptr), here we have to pay attention to the loop body in the link direction otherwise it is likely to appear useless pointers cause errors, because finally we want to leave a node so ptr- >next is null, and the rest cannot be null->next. Specific procedures, as follows: #include <stdio.h>
#include <stdlib.h>struct node
{
int num;
struct node *next;
};struct node *head;
struct node *last;void cre_list ()
{
Head = (struct node *) malloc (sizeof (struct node));
last = head;
Head->next = head;
} void Display_node ()
{
struct node *ptr = head->next;
while (ptr! = head)
{
printf ("%d\t", ptr->num);
PTR = ptr->next;
}
printf ("\ n");
}void add_node (int num)
{
struct node *ptr = (struct node *) malloc (sizeof (struct node));
Ptr->num = num;
Ptr->next = head;
Last->next = ptr;
last = ptr;
}void Rev_node ()
{
struct node *ptr = head;
Last->next = head->next;
Head = head->next;
Free (PTR);
}void Tiren_node ()
{
struct node *ptr = head;
struct node *str = ptr->next;
struct node *qtr = str->next;
while (ptr->next! = ptr)
{
str = ptr->next;
Qtr = str->next;
Str->next = qtr->next;
PTR = str->next;
}
printf ("%d\n", ptr->num);
}int Main ()
{
int i = 0;
Cre_list ();
int n;
printf ("Please input n:\n");
scanf ("%d", &n);
printf ("%d\n", N);
for (i = 1;i <= n;i++)
{
Add_node (i);
}
Display_node ();
Rev_node ();
Tiren_node ();
return 0;
}

"Turn" Joseph Ring algorithm---------topic: There are n people around in a circle, order automatic arranging, from the first start off (from 1 to 3 off), where 3 of the people quit the circle, ask the last left is the original number of the first.

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.