A one-way circular linked list Introduction
Two, annular one-way linked list scenario
Josephu (Joseph, Joseph Central) problem
Problem Description : Let numbered 1,2, ... n, n individuals sitting around the agreed number k (1 <= k <= n) of the person from a start number of packets, number of the person to m column, which is the next and from 1 Countin, that person has a number to m columns, once on, until everyone out of the line up, thereby generating a sequence number of the team.
Tip : with a circular list does not lead to processing node Josephus problems: constituting a first single cycle list of n nodes, then the node k 1 from the start counting when the count to m, corresponding to the node removed from the list, and from 1 then starts counting from the deleted node to the next node, until the end of the last node to delete from the list.
Three, Josephu problem
1, a schematic view of Joseph
2, Josephu problem
Summary of issues: Let numbered 1,2, ... n, n individuals sitting around a circle, about the point number k (1 <= k <= n) from a start number of people reported that the number of people to m column, and from it the next start number 1 reported that the number of individuals to m and the columns, and so on, until everyone out of the line up, thereby generating a sequence number of the team
3 Tips
With a circular list does not lead to processing node Josephus problems: constituting a first single cycle list of n nodes, starting from k and then start counting from 1, when the count to m, corresponding to the node removed from the list, and then and from 1 to start counting from the deleted node to the next node, until the end of the last node to delete from the list.
4, Joseph problem - the idea to create a circular linked list illustration
5, Joseph problems - a circle of children thinking analysis chart
6, code implementation
. 1 Package com.java.linkedlist;
2
. 3 public class Josepfu {
. 4
. 5 public static void main (String [] args) {
. 6 // test
7 // Construction circular linked list
. 8 CircleSingleLinkedList List = new new CircleSingleLinkedList ();
. 9 list.addBoy (5); // Add 5 child nodes
10 list.showBoy ();
. 11
12 is // test the ring
13 is list.countBoy (. 1, 2, 5 );
14
15 }
16
. 17 }
18 is
. 19 // Create annular singly linked list
20 is class CircleSingleLinkedList {
21 is // Create a first node, the current number is not
22 is Private Boy first = null ;
23 is
24 // add a child node to form an annular chain
25 public void addBoy ( int nums) {
26 is // nums data check
27 IF (nums <. 1 ) {
28 System.out.println ( "nums incorrect value" );
29 return ;
30 }
31 is
32 Boy curBoy = null ; // secondary pointer, help build endless chain
33 // used for creating circular linked list
34 is for ( int I =. 1; I <= the nums; I ++ ) {
35 // The number created child nodes
36 Boy = Boy new new Boy (I);
37 [ // If this is the first child
38 is IF (I ==. 1 ) {
39 first = Boy;
40 first.setNext (first); // constituting a ring
41 is curBoy = first; / / let curBoy points to the first child
42 } The else {
43 is curBoy.setNext (Boy);
44 is boy.setNext (First);
45 curBoy = Boy;
46 is }
47 }
48
49 }
50
51 is // traverse the linked list of the current loop
52 is public void showBoy () {
53 is // determines whether the list is empty
54 is iF (First == null ) {
55 System.out.println ( "no children" );
56 is return ;
57 is }
58
59 // because not move first, complete traversal auxiliary pointers
60 Boy curBoy = first;
61 is the while ( to true ) {
62 is System.out.printf ( "number of child% D \ n-" , curBoy.getNo ()) ;
63 is IF (curBoy.getNext () == First) { // description has completed traversing
64 BREAK ;
65 }
66 curBoy curBoy.getNext = (); // after allowing curBoy shifted
67 }
68 }
69
70 // user input, calculates a sequence of a circle
71 / **
72 *
73 is * @param startNo expressed from several children began to count
74 * @param countNum represents a few
75 * @param the nums initially indicates the number of children in the circle
76 * /
77 public void countBoy ( int startNo, int countNum, int the nums) {
78 // first check data
79 IF (first == null || startNo <startNo. 1 ||> the nums) {
80 System.out.println ( "incorrect input parameters, Please re-enter " );
81 return;
82 }
83
84 // create a secondary pointer, to help the children complete the ring
85 Boy Helper = First;
86 // need to create a secondary pointer Helper, prior to this last point should be a circular linked list node
87 the while ( to true ) {
88 IF (helper.getNext () == first) {
89 BREAK ;
90 }
91 is helper = helper.getNext ();
92 }
93
94 // first few packets children, and let the first mobile helper k-1 times
95 for ( int j = 0; j <startNo - 1; j ++) {
96 first = first.getNext ();
97 helper = helper.getNext ();
98 }
99
100 // when the number of packets children, and let the first helper while moving the pointer m-1 times, and then the ring
101 // cycle of operation, until only one node in circle
102 the while ( to true ) {
103 IF (first helper ==) { // description, only one node ring
104 BREAK ;
105 }
106 // let pointer movement while first and helper countNum -1 Ci
107 for ( int J = 0; J <countNum -. 1; J ++) {
108 first = first.getNext ();
109 Helper = helper.getNext ();
110 }
111 // At this point the first node, the child node is to the circle
112 System.out.printf ( "% d child a ring \ n-" , first.getNo ());
113 // At this point the first child node of the ring
114 first = first.getNext ();
115 helper.setNext (first);
1 16 }
117
1 18 the System.out .printf ( "last child left in the circle D number% \ n-" , first.getNo ());
119 }
120 }
121
122 // Create a Boy class, represents a node
123 class Boy {
124 Private int NO; // number
125 Private Boy Next; // to point to the next node, the default null
126
127 // constructor
128 public Boy ( int NO) {
129 the this .no = NO;
130. }
131 is
132 public int getNo () {
133 return NO;
134 }
135
136 public void SetNo ( int no) {
137 this.no = no;
138 }
139
140 public Boy getNext() {
141 return next;
142 }
143
144 public void setNext(Boy next) {
145 this.next = next;
146 }
147
148 }