Method example of solving the problem of losing handkerchief based on bidirectional ring linked list in Java

This paper describes the method of solving the problem of losing handkerchief based on bidirectional ring linked list in Java. Share with you for your reference, as follows:

Question: let several children numbered 1, 2... N sit around. It is agreed that the child numbered K (1 = < K < = n) will count off from 1, and the one counted to m will be listed. His next digit will count off from 1, and the one counted to m will be listed again until everyone is listed, resulting in a sequence of out of line numbers.

We now use a two-way circular linked list to solve this problem. Let's take a look at the following picture:

The circle represents a node. The red pointer points to the next element and the purple pointer points to the previous element. The first pointer points to the first element, indicating the position of the first element. Cursor is a cursor pointer, which plays an important role. Then the circular linked list can simulate the circle formed by children. The following is the specific code:

The basic framework of this code is based on Han Shunping's video. However, he uses a one-way linked list. The above code comments are used to find the previous element of the element pointed to by cursor, which is realized by turning the whole linked list around. Here I changed it to a two-way linked list and directly used a cursor Previouschild is fine.

Operation results:

For more information about Java algorithms, readers who are interested can see the topics on this site: Java data structure and algorithm tutorial, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>