Joseph Ring [application of circular linked list]

Source: Internet
Author: User


Joseph PHY problem Overview: Assume that N people are sitting around the Round Table, report data from S people, report data to M columns, and then count the report from the next person, when the number is M, the columns are displayed. Each person's information must be output sequentially in the column!
# 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;
Clinklist creatclinklist (int );
Clistnode * p, * q, * r, * t;
Head = creatclinklist (n );
Q = head;
If (s> n) printf ("error! N ");
For (I = 1; I <s; I ++)
Q = q-> next;
P = q-> next;
For (I = 1; I <= n; I ++)
{
J = 1;
While (j <m)
{
R = p;
P = p-> next;
If (p = head)
Continue;
J ++;
}
Printf ("% 4d", p-> data );
T = p;
If (p-> next = head)
{
P = p-> next;
R-> next = head;
}
Else
{
P = p-> next;
R-> next = p;
}
Free (t );
}
}
Clinklist creatclinklist (int n)
{
Int I;
Clinklist p;
P = (clinklist) malloc (sizeof (clistnode ));
P-> next = NULL;
Head = p;
Printf ("Please input the element records of the chain! N ");
For (I = n; I> 0; I --)
{P = (clinklist) malloc (sizeof (clistnode ));
Scanf ("% d", & p-> data );
If (n = I)
{
P-> next = head;
Head-> next = p;
}
Else
{
P-> next = head-> next

<

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.