Circular Linked List Implementation of Joseph Ring

Source: Internet
Author: User

design a cyclic single-chain table class at the leading node to implement the Joseph Ring problem.
Problem description: set the number to 1, 2 ,..., N (n> 0) Individuals sit in a circle clockwise and each person holds a positive integer password.
at the beginning, an upper value of M is given at any time. The value ranges from 1 clockwise to the first person.
when reporting to m, the report is stopped. The reporter of M is listed and his password is used as the new M value, from the next person in the clockwise direction, the number of new records starting from 1.
This continues until all users are listed. Design a Program to simulate this process and provide the serial number sequence of the specified person.
test data: the passwords of N = 7 and 7 are respectively 3, 1, 7, 2, 4, and 4. The upper limit is M = 20.
correct sequence: 6, 1, 4, 7, 2, 3, 5

# Include <stdio. h> # include <malloc. h> struct lList {int number; // number int psw; // password struct lList * Next;}; typedef struct lList node; typedef node * llink; // llink createlist (INT total) // {// llink newnode; // llink before, firstnode; // int I; // firstnode = (llink) malloc (sizeof (node); // If (! Firstnode) // return firstnode; // firstnode-> Number = 1; // printf ("enter the password of the 1st person:"); // scanf ("% d ", & firstnode-> psw); // firstnode-> next = NULL; // before = firstnode; // for (I = 2; I <= total; I ++) // {// newnode = (llink) malloc (sizeof (node); // If (! Newnode) // return NULL; // newnode-> Number = I; // printf ("Enter the personal password of % d:", I ); // scanf ("% d", & newnode-> psw); // newnode-> next = NULL; // before-> next = newnode; // newnode = before; //}// newnode-> next = firstnode; // return firstnode; //} llink createlist (INT total) {llink newnode; llink before, firstnode; int I; firstnode = before = (llink) malloc (sizeof (node); If (! Before) return before; before-> Number = 1; printf ("enter the password of the first 1st people:"); scanf ("% d", & before-> psw ); for (I = 2; I <= total; I ++) {newnode = (llink) malloc (sizeof (node); If (! Newnode) return NULL; newnode-> Number = I; printf ("Enter your personal password for % d:", I); scanf ("% d ", & newnode-> psw); before-> next = newnode; before = newnode;} before-> next = firstnode; return firstnode;} void Joseph push (llink L) {int I, m; llink p, q, s; P = L; printf ("Enter the initial password:"); scanf ("% d", & M ); printf ("the column order is:"); While (p-> next! = P) {for (I = 1; I <m; I ++) {q = P; P = p-> next;} printf ("% 5d ", p-> Number); M = p-> psw; S = P; q-> next = p-> next; P = p-> next; free (s );} printf ("% 5d", p-> Number); printf ("\ n");} void main () {llink L; int N; printf ("Number of students: "); scanf (" % d ", & N); L = createlist (n); Joseph push (l );}

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.