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.