Data Structure-Joseph Ring

Source: Internet
Author: User

/// Joseph Ring
// Time: 05/07/04
// Program: Zhang jianbo

// Input n = 7 // 3, 1, 7, 2, 4, 4 // output 6 7 4 1 5 3 2

# Include <iostream. h>
# Include "key. H"

Typedef struct person
{Int PWD; // Password
Int num; // number of people
Struct person * Next; // pointer to the struct
} Person;

 

Void output (int * a, int N); // output result
Int creatpersonlist (person * head); // create a linked list
Int fx_personlist (person * head, int N, int * A); // analog report count

Int _ f4_main () // function entry
{

Person * head; // defines the head of the linked list.

Cout <"Joseph Ring problem simulation! "<Endl;

Head = new person; // create a person and allocate memory

Int n = creatpersonlist (head); // create a linked list and return n people

Int * A = new int [20]; // Save the output result

Int x = fx_personlist (Head, n, a); // simulate the number of records and return the number of outgoing records count

Output (A, x); // output sequence

Initkey (); // keyboard interrupt

Return 0;
}

Int creatpersonlist (person * head ){
Int n, m, I;
Person * P, * q;
Cout <"/N, enter n = ";
Cin> N;
P = head; // P points to the header
For (I = 1; I <= N; I ++) {// create a linked list
For cout <"/N, enter the" <I <"reporter password M = ";
Cin> m;
P-> Pwd = m;
P-> num = I;
Q = new person;
If (I = N) P-> next = head; // when I = N, Point P1 to the header at the end of the loop
Else
{
P-> next = Q;
P = Q;
}

}
Return N;

}

Int fx_personlist (person * head, int N, int * ){
Person * P1, * P0, * P;
Int TM; // temporary variable, save the previous password value
Int m; // password m
Int COUNT = 1; // Number of outgoing queues

P0 = p1 = head; // make P0, P1 point to head Header

Cout <"/n enter the start value of M = ";
Cin> m;
While (count <= N-1)
{
For (INT I = 1; I <m; I ++)
{// Re-use m as the upper limit of the report count
P1 = P0;
P0 = P0-> next;
TM = P0-> pwd;
}
M = TM;
P = P0-> next;
A [count ++] = P0-> num; // Save the serial number of each departure in a [x]
P1-> next = P0-> next;
Delete P0;
P0 = P;
}
A [count] = P0-> num; // Save the serial number of each departure in a [x]
P1-> next = P0-> next;
Delete P0;

Return count;
}

Void output (int * a, int N)
{
Cout <"the correct output sequence is :";
For (INT I = 1; I <= N; I ++) cout <A [I] <"";

}

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.