C Language Simulation of Joseph Ring

Source: Internet
Author: User

I,Requirement Analysis

1. in this demo, the number of nodes and password of Joseph cannot exceed 100, and the password of each node cannot exceed twice the number of nodes, for convenience, all passwords are dynamically generated based on time. The program needs to enter: ① number of nodes that generate the Joseph ring; ② initial password of the program; if the input exceeds the limit, the default value is 10.

2. commands executed by the program include: Entering the node and initial password of the Joseph ring, initializing and dynamically generating the Joseph ring, and running demonstration of the Joseph ring.

3. program test: correct input test, condition boundary input test, and over boundary input test

II,Outline Design

4. To implement the topic requirement function, a one-way circular linked list is used to represent the Joseph ring.

5. If a link is not released in the Joseph ring, the node is released in the linked list.

6. When the last node is left, we will not release it, but output it to get the desired result!

III,Detailed Design

1. Define the node data structure:

Typedef struct {

Int num;/* The num for Joseph struct */

Int password;/* Password for Joseph struct */

} SElemType;

2. Define the node struct to create a circular linked list:

Typedef struct Joseph {/* The Node for the ring of Joseph */

SElemType elem;

Struct Joseph * next;

} Joseph;

3. Define the node initialization function to initialize the linked list node:

Int InitList (Joseph * l) {/* Initial the node */

L = (Joseph *) malloc (sizeof (Joseph ));

If (! L)

Return OVERFLOW;

Return OK;

}

4. Space release of Joseph linked list nodes:

Void FreeNode (Joseph * p) {/* Free the node have been deleted */

Joseph * temp = p;

While (temp-> next! = P)

Temp = temp-> next;

Temp-> next = p-> next;

Printf ("The node of num: % 3d is free" n ", p-> elem. num );

Free (p );

}

5. Create the Joseph linked list and randomly initialize the data field data of the node:

Void Creat_Joseph (Joseph * l, int length) {/* creat the password of node by rand ()*/

Int I;

Joseph * p = l;

L-> next = l;

L-> elem. num = 1;

Srand (unsigned) time (NULL ));

L-> elem. password = rand () % (length + 3) + 1;

Printf ("The flowing is the Joseph Ring created by rand ():" n ");

Printf ("The Node num: % 3d Password: % 3d" n ", l-> elem. num, l-> elem. password);/* print the first node of the ring of Joseph */

For (I = 1; I <length; I ++ ){

Joseph * temp = (Joseph *) malloc (sizeof (Joseph ));

P-> next = temp;

Temp-> next = l;

Temp-> elem. num = I + 1;

Temp-> elem. password = rand () % (2 * length) + 1;

Printf ("the node num: % 3d password: % 3d" N ", temp-> ELEM. num, temp-> ELEM. password);/* print all the node of the ring of Joseph each CT the first one */

P = p-> next;

}

}

6. Demonstration of the actual process of Joseph's problem:

Void Joseph ph_run (Joseph * l, int m) {/* Do node free by password of node */

Int I, K;

Joseph * temp, * P;

If (L-> next = L)

Printf ("the last node num is: % d", L-> ELEM. Num);/* print the last node */

Else {

Temp = L;

For (I = 1; I <m; I ++)

Temp = temp-> next;

P = temp-> next;

K = temp-> elem. password;

FreeNode (temp);/* Free The unusefull Node */

Joseph ph_run (p, k);/* Using The Recursive Functions */

}

}

7. Description of the main function:

Void main (){

Int num = 10;

Int m = 10;

Joseph * J_list;

Printf ("Please input the total num of Joseph Nodes (less than 100):");/* input the total num of Joseph Nodes */

Scanf ("% d", & num );

If (num <= 0 | num> = 100 ){

Printf ("What your input is not accepted, so using 10 instead! "N");/* Using the default to instead */

Num = 10;

}

Printf ("Please input the first node num (less than 100):");/* input the first node num */

Scanf ("% d", & m );

If (m <= 0 | m> = 100 ){

Printf ("What your input is not accepted, so using 10 instead! "N");/* Using the default to instead */

M = 10;

}

InitList (J_list );

Creat_Joseph (J_list, num );

Joseph ph_run (J_list, m );

}

Source code download

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.