Joseph PHY (application of circular linked list)

Source: Internet
Author: User


Joseph PHY: Assume that N people are sitting around the Round Table, reporting data from S people, and reporting data to M,
Then report the number from the next person, and the number of M people will be listed as follows:
Column. Requires that the information of each person be output in sequence of Columns

# Include <stdio. h>
# Include <stdlib. h>
Typedef int datatype;
Typedef struct cnode
{
Datatype data;
Struct cnode * next;
} Clistnode;
Typedef clistnode * clinklist;
Clinklist head;
Void Joseph (int n, int m, int s)
{
Int I, j, flag;
Clinklist creatclinklist (int);/* function declaration: Creates a circular linked list using the element forward insertion method. The length is limited to N */
Void print (clinklist head, int );
Clistnode * p, * q, * r, * t;
Head = creatclinklist (n );
Print (head, n);/* print the elements of the circular linked list in order */
Q = head;
If (s> n) printf ("error! N ");
Flag = s % n;/* move the pointer q to the second node and give it to the pointer p to minimize the number of pointer moves! */
For (I = 1; I <flag; I ++)
Q = q-> next;
P = q-> next;
Printf ("% d node in the order of departure is; n", n );
For (I = 1; I <= n; I ++)/* records I as the number of deleted nodes */
{
J = 1;
Flag = m % (n-I + 1);/* move the pointer p to the M node to minimize the number of pointer moves! Try to make the number of moves per time
No more than n times! Time complexity O (n), improving the execution efficiency of the program! */
If (! Flag)/* do not know how to move this step! Special Handling of some nodes! */
{
J = 1;
While (j <m)
{
R = p;
P = p-> next;
If (p = head)
Continue;
J ++;
}
}

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.