Joseph Ring queue + linked list, Joseph Ring queue

Source: Internet
Author: User

Joseph Ring queue + linked list, Joseph Ring queue
There are n people in a circle in turn, starting from 1st people, counting to m people, then starting from the next person in the column, counting to m people again ,..., This is repeated until all people are listed. Set the numbers of n persons to 1, 2 ,..., N, print the column order.[Algorithm analysis]In this question, we can use arrays to establish a flag and other methods to solve the problem. However, if we use the idea of loop chain in the data structure, it is more appropriate to the question and the efficiency of solving the problem is higher. In a circle of n people, one person is regarded as a node, and the relationship between n people is linked, that is, each node has a forward node and a next node, each node has a pointer pointing to the next node, and the last node pointer pointing to the first node. This is the data structure of a single loop chain. When m people leave the column, point the cursor of the m node to the next node pointer of the m node, that is, drive the m node out of the loop chain. 1. Create a circular linked list. When using arrays to implement the chained structure of this question, array a [I] is used as the "Pointer" variable, and a [I] stores the location of the next node. When pointer j is set to point to the current node, the process of moving the node is j = a [j]. When the number of m points, m points out the chain, then a [j] = a [a [j]. When the chain is used directly to realize the current situation, it is more intuitive. Each node has two fields: one numeric field and one pointer field. When the number of m nodes is reached, the m node outputs the chain, point the forward and backward node pointer of the m node to its successor node; 2. Set up a pointer to point to the current node, set up a counter, and count the number of people; 3. Move the pointer along the chain, every time a node is moved, the counter value is added to 1. When the counter value is m, m is terminated and the counter value is set to 1. 4. Repeat 3 until n nodes are all out of the chain.

1 # include <iostream> 2 # include <cstdio> 3 using namespace std; 4 int a [0, 10000001]; 5 int tot = 1; 6 int main () 7 {8 int n, m; 9 cin> n> m; 10 int j = n; 11 for (int I = 0; I <n; I ++) 12 {13 a [I] = I + 1; 14} 15 a [n] = 1; 16 int k = 1; // Report Number 17 while (tot <= n) 18 {19 while (k <m) 20 {21 j = a [j]; 22 k ++; 23} 24 // cout <a [j] <"; 25 printf (" % d ", a [j]); 26 a [j] = a [a [j]; 27 k = 1; 28 tot ++; 29} 30 return 0; 31}

 

Related Article

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.