A Brief Talk on Data Structure-Joseph Ring
What is Joseph Ring?
- Description of the problem, there is a scene: a group of children in the playground surrounded by a circle, they agreed to report from a person from the beginning (similar to the number, 1, 2, 3,... n) It is stipulated that the k-th person will be listed on the list, and the rest will continue to form a circle and start counting until all the people are listed, and Joseph's ring will end.
Begin to understand Joseph's rings.
1. My idea, in fact, through the above description of the problem, you have a basic understanding of the basic characteristics of Joseph's ring, I believe that students who understand the ring list will feel that this is not a single ring list? Yes, it's a so-called ring list. (But that's OK. For those who don't know anything about it, you can see my blog later.) Ok, so that we can start to analyze the problem.
Suppose there are five children, as follows:
Five of them are in a circle. Let's assume that from boy1 we start counting, one person out of every three, until all of them are out of the circle. Now we can calculate the results first by ourselves. I've written them here, and the students can try to see if my results are right.
The expected results are: 3 - > 1 - > 5 - > 2 - > 4
ok, let's do a code demonstration
Java Code Implementation and Principle Analysis of Joseph Ring
1. Then the above questions begin to be analyzed.
First, since the single list node is boy, we must have a Boy class to place its properties.
The code is as follows:
//Create a Boy class to represent a node class Boy{ private int no;//Represents boy number private Boy next;//Represents the next node of the current boy (similar to a pointer) //Adding constructors and getter and setter methods public Boy(int no){ this.no=no; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public Boy getNext() { return next; } public void setNext(Boy next) { this.next = next; } }
Next, we build a circular one-way linked list, CircleSingle LinkedList, and define some methods
The code is as follows:
//Create a ring one-way list class CircleSingleLinkedList{ //Create a first node, which is not currently numbered. private Boy first=new Boy(-1); //Add child nodes to form a ring list public void addBoy(int nums){ if(nums<1){ System.out.println("afferent Nums It's wrong!"); return; } Boy curBoy=null;//Auxiliary Variables to Help Build Ring Lists //Create a ring list with for for(int i=1;i<=nums;i++){ //Create child nodes by number Boy boy=new Boy(i); //If it's the first child if(i==1){ first=boy; first.setNext(firs*t);//Constitutive Ring curBoy = first;//Let curBoy point to the first child }else { curBoy.setNext(boy); boy.setNext(first); curBoy=boy; } } } //Traversing the current ring list public void showBoy(){ //Judging whether the list is empty if(first==null){ System.out.println("The list is empty"); return; } //Because first cannot move, traverse with auxiliary pointers Boy curBoy=first; while (true){ System.out.printf("Number of the child%d%n",curBoy.getNo()); if(curBoy.getNext()==first){ //It's done. break; } curBoy=curBoy.getNext();//curBoy moved back } } //According to the user's input, calculate the order of children out of the circle /** * * @param startNo Reporting the number of children from the beginning * @param countNum Represent several times * @param nums Indicates how many children were in the circle in the first place. */ public void countBoy(int startNo,int countNum,int nums){ //Check the data first if(first==null || startNo<1 || startNo>nums){ System.out.println("There is a mistake in parameter input. Please re-enter it."); return; } //Create auxiliary pointers to help children out of the circle Boy helper=first; //Let helper point to the last node of the ring list while (true){ if(helper.getNext()==first){ //Explanation points to the last node break; } helper=helper.getNext(); } //Let first and helper move startNo-1 before the child counts. for(int j=0;j<startNo-1;j++){ first=first.getNext(); helper=helper.getNext(); } while (true){ if(helper==first){ //It means there's only one person. break; } //Let first and helper pointers move countNum - 1 time at the same time for(int j=0;j<countNum-1;j++){ first=first.getNext(); helper=helper.getNext(); } //At this point first points to the node where the child wants to go out of the loop. System.out.printf("Child%d Circle out\n",first.getNo()); first=first.getNext(); helper.setNext(first);//Connect the circle } System.out.printf("The last child to remain in the circle is numbered as ___________%d\n",first.getNo()); } }
ok, you may be a little confused after reading this code. That's all right. I'll solve the confusion for you next. First, look at the picture below.
At first, the auxiliary variable first points to the boy node of the first person to report. Note that this does not necessarily point to boy1, because it does not necessarily start with boy1, and the helper points to the last boy node.
Next, let's take a step and see what happens.
As follows:
Here, you have also noticed that when you count to 3, the first point should be boy3, and the helper point to boy2. Here I take boy3 out and let the code go down. So first now points to next of boy4 and helper to first, so that the linked list can continue to connect and see the code comments concretely. It's easy to understand.
ok, let's test it.
The code is as follows:
public class Josepfu { public static void main(String[] args) { //test CircleSingleLinkedList circleSingleLinkedList=new CircleSingleLinkedList(); circleSingleLinkedList.addBoy(5); circleSingleLinkedList.showBoy(); circleSingleLinkedList.countBoy(1,3,5); } }
Output printing results
Number 1 of the child Number 2 of the child Number 3 of the child Number 4 of the child Number 5 of the child Children 3 out of the loop Kid 1 out of circle Children 5 out of circle Children 2 out of the loop The last child left in the circle is numbered 4
ok, check it out here! No problem with the code.
I am also the first time to write a blog, recently I am learning data structure, thinking of learning while consolidating.
I hope you can comment positively on the shortcomings, and then I will develop the habit of blogging. Finally, I hope you can grow up together and excel together!!!