Joseph's c linked list Solution Program-(0)

Source: Internet
Author: User



1, 2 ,..., n of the n people sit at the Round Table clockwise. Each person holds a password (positive integer), starts reporting the number of s people, reports the number to the m people, and reports the number from the next person, until all members are listed
Below is the C language source program compiled with a linked list.
# Include <stdio. h>
# Include <malloc. h>
 
Typedef struct Joseph
{
Int num;
Struct Joseph * next;
} Joseph, * linklist;
 
 
Main ()
{
Int I, j, n, s, password;
Struct Joseph * head, * p1, * p2;
Head = (struct Joseph *) malloc (sizeof (Joseph ));
P1 = (struct Joseph *) malloc (sizeof (Joseph ));
P2 = (struct Joseph *) malloc (sizeof (Joseph ));
Printf ("input the value of n :");
Scanf ("% d", & n );
Printf ("input the number of the beginer :");
Scanf ("% d", & s );
If (s> n)
{
Printf ("please input a number smaller than % d:", n );
Scanf ("% d", & s );
}
Printf ("input the value of the password :");
Scanf ("% d", & password );
Printf ("the output is: n ");
 
P1-> num = 1;
P2-> num = 2;
Head-> next = p1;
P1-> next = p2;
P1 = p1-> next;
For (I = 3; I <= n; I ++)
{
P2 = (struct Joseph *) malloc (sizeof (Joseph ));
P1-> next = p2;
P1 = p1-> next;
P1-> num = I;
If (p1-> num = n)
P1-> next = head-> next;
}
 
P1 = head-> next;
For (I = 1; I <s; I ++)
P1 = p1-> next;
For (I = 1; I <= n; I ++)
{
If (password! = 1)
{
For (j = 1; j <password-1; j ++)
P1 = p1-> next;
Printf ("% 4d", p1-> next-> num );
P1-> next = p1-> next;
P1 = p1-> next;
}
 
If (password = 1)
{
Printf ("% 4d", p1-> num );
P1 = p1-> next;
}
}
Printf ("nn ");

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.