Initial solution to the Linked List (iii) -- circular Linked List Implementation of Joseph's ring

Source: Internet
Author: User

Joseph's ring is an application of mathematics: n people (represented by numbers 1, 2, 3... n) are known to be sitting around a round table. The number of people numbered k starts to report, and the person counting to m is listed; his next person starts from 1

The number of people who count to m is displayed again. Repeat this rule until all the people around the Round Table are listed. Now we are writing a circular linked list program to implement the Joseph Ring problem and output the result of each column output ~

Use a circular linked list to simulate this process: 1. Create a table; 2. simulate a column rule.

The following is an old routine: directly paste the source code + notes ~

Code:

# Include <iostream> using namespace std; typedef struct List {int data; struct List * next;} List, * Lnode; void Joseph PHUs (int n, int k, int m) // n: There are n people, numbered 1-n; k: the number of people numbered k starts to report; m: The number of people counting m is displayed. {Lnode first, now, pre, temp; // first: Header node; now: always represents the current pointer position; pre: now's precursor node; temp: new node. // Create a circular linked List first = (Lnode) malloc (sizeof (List); first-> data = 1; first-> next = first; now = first; // create a circular linked List numbered 1-n for (int iFirst = 2; iFirst <= n; iFirst ++) {temp = (Lnode) malloc (sizeof (List )); temp-> data = iFirst; // number each node 1-n. // Assign the successor of the current pointer to the successor of the newly created node (that is, the successor of the new node is the default "header node" first ). Temp-> next = now-> next; now-> next = temp; // fix the disconnected connection. Now = temp; // Move the current pointer to continue the table creation process .} // Initialize the current pointer position and point to the position numbered 1. Now = now-> next; // The current Pointer Points to a position numbered k. While (-- k) {pre = now; now = now-> next;} // simulate the column process. Printf ("The Line is:"); while (n --) {for (int j = 1; j <m; j ++) {// The current Pointer Points to the person in the m report. Pre = now; now = now-> next;} // Delete and output the column number. Pre-> next = now-> next; if (now-> next = now) printf ("% d \ n", now-> data ); // line feed when there is one remaining node. Else printf ("% d->", now-> data); first = now-> next; // The first person who reports the number after the column is displayed. Free (now); // release the deleted node. Now = first; // The current pointer is transferred to the first reporter to continue the columns process .}} Int main () {Joseph PHUs (9, 1, 5); return 0 ;}


Ps: for reference only ~~

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.