单向环形链表
Josephu 约瑟夫环问题
编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m的那个人出列,他的下一位又从1开始报数,报到m的那个人出列,以此类推,直到所有人出列,由此产生一个出队编号的序列
构建单向环形链表
- 先创建第一个节点,让 first 指向该节点,并形成环形
- 后面当每创建一个新的节点,就把该节点加入到已有的环形链表中即可
遍历环形链表
- 先让一个辅助指针(变量),指向 first 节点
- 然后通过一个 while 循环变量该环形链表即可; curNode.next == first 结束
出队列
-
需要创建一个辅助指针(变量)helper,事先指向环形链表的最后一个节点
报数前,先让 first 和 helper 移动 k-1 次
-
当小孩报数时,让 first 和 helper 指针同时的移动 m-1 次
-
这是可以讲 first 指向需要出列的小孩
-
出列:
first = first.next
helper.next = first
原来 first 指向的节点就没有任何引用,被垃圾回收
代码实现
package doublelinkedlist;
public class CircleLinkedList {
public static void main(String[] args) {
CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
circleSingleLinkedList.addBoy(25);
circleSingleLinkedList.showBoy();
circleSingleLinkedList.countBoy(1,2,25);
}
}
// create circle single linked list
class CircleSingleLinkedList{
// create a first node
private Boy first = null;
// add boy nodes
public void addBoy(int nums){ // nums: # of nodes
// check nums
if(nums < 1){
System.out.println("nums incorrect");
return;
}
Boy curBoy = null; // auxiliary pointer
// for loop create circle single LL
for (int i = 1; i <= nums; i++) {
Boy boy = new Boy(i);
if(i==1){
first = boy;
first.setNext(first); // form a circle with one element
curBoy = first;
}else{
curBoy.setNext(boy);
boy.setNext(first);
curBoy = boy;
}
}
}
// traverse list
public void showBoy(){
// whether the list is empty?
if(first == null){
System.out.println("empty");
return;
}
Boy curBoy = first;
while(true){
System.out.printf("id of boy is %d\n",curBoy.getId() );
if(curBoy.getNext()==first){ // traversal done
break;
}
curBoy = curBoy.getNext();
}
}
// pop out list
/**
*
* @param startId start from which id
* @param countNum count how many times
* @param nums # of id in the list initially
*/
public void countBoy(int startId, int countNum,int nums){
// check data
if(first == null || startId<1 || startId>nums){
System.out.println("Some wrong with parameters");
return;
}
// create a helper pointer
Boy helper = first;
while(true){
if(helper.getNext()==first){ // helper point to the last boy
break;
}
helper = helper.getNext();
}
// before function goes, move first and helper k-1 step
for (int j = 0; j < startId-1; j++) {
first = first.getNext();
helper = helper.getNext();
}
// when boy reported, move first and helper m-1 step at the same time
// loop, till there only 1 boy left in the list
while(true){
if(helper == first){ // only 1 node left
break;
}
// move helper and first countNum-1 step
for (int i = 0; i < countNum-1; i++) {
first = first.getNext();
helper = helper.getNext();
}
// now, first point to the node should be popped out
System.out.printf("child %d go out\n",first.getId());
// move the node pointed by first out
first = first.getNext();
// helper.next = first; next is private;
helper.setNext(first);
}
System.out.printf("the last node is %d\n",first.getId());
}
}
// create a class named boy, means a node
class Boy {
private int id;
private Boy next; // point to next node, default null
public Boy(int id){
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Boy getNext() {
return next;
}
public void setNext(Boy next) {
this.next = next;
}
}