环形链表
Josephu问题
设编号为1, 2, …, n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,依此类推,直到所有人出列为止,由此产生一个出队编号的序列。
构建单向环形链表
- 先创建第一个节点,让first指向该节点,并让该节点的next指向自身形成环形
- 后面当我们每创建一个新的节点,就把该节点,加入到已有的环形链表中即可
节点类:
//创建一个Boy类,表示一个节点
class Boy {
private int no;//编号
private Boy next;//指向下一个节点,默认null
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;
}
}
代码实现:
/**
* 添加小孩节点,构建成一个环形链表
*/
public void addBoy(int nums) {
//nums做数据校验
if (nums < 1) {
System.out.println("nums的值不正确");
return;
}
Boy curBoy = null;//辅助指针,帮助构建环形链表
for (int i = 1; i <= nums; i++) {
//根据编号,创建小孩节点
Boy boy = new Boy(i);
//添加新节点
//如果是第一个节点
if (1 == i) {
first = boy;
first.setNext(first);//指向自己,构成环状
curBoy = first;//让curBoy指向第一个
} else {
curBoy.setNext(boy);
curBoy = boy;//让curBoy指向新添加的节点
curBoy.setNext(first);//新添加的节点next指向first节点
}
}
}
遍历
- 先让一个辅助指针currBoy指向first节点
- 然后通过一个while循环遍历该链表即可currBoy.next == first遍历结束
代码实现:
public void showBoy() {
if (null == first) {
System.out.println("链表为空");
return;
}
Boy curBoy = first;//辅助指针,帮助遍历
do {//先执行一次再判断
System.out.println("节点的编号:" + curBoy.getNo());
curBoy = curBoy.getNext();//当前节点前移
} while (curBoy != first);//下一个节点是first节点时停止遍历
}
解决Josephu问题
根据用户输入生成节点出圈顺序
- 创建一个辅助指针helper,主其指向环形链表的最后节点
- 先first和helper移动startNo-1次,分别移动到开始节点和开始节点的前一个节点
- 开始报数,让first和helper移动countNum-1次,将first指向的节点出圈
- 直到圈中只有一个节点时结束。
代码实现:
/**
* 根据用户的输入,计算出圈的顺序
*
* @param startNo 表示从第几个小孩开始数
* @param countNum 表示数几次
* @param nums 表示最初圈中有多少小孩
*/
public void josephu(int startNo, int countNum, int nums) {
if (first == null || startNo < 1 || startNo > nums) {
System.out.println("参数输入有误,请重新输入");
return;
}
Boy helper = first;//辅助指针
//让helpBoy指向最后一个节点
while (helper.getNext() != first) {//下一个节点是first节点时停止移动
helper = helper.getNext();//当前helpBoy前移
}
//报数前,先让first移动到开始报数的节点,helpBoy到开始报数的前一个节点,startNo-1次
for (int i = 0; i < startNo - 1; i++) {
helper = helper.getNext();
first = first.getNext();
}
//开始报数出圈操作
while (first != helper) {//圈中只有一个人
//移动first到出圈的节点,移动helper出圈的节点的前一个节点
for (int i = 0; i < countNum - 1; i++) {
helper = helper.getNext();
first = first.getNext();
}
//first指向的就是要出圈的节点
System.out.println("出圈节点的编号为:" + first.getNo());
//出圈操作
first = first.getNext();
helper.setNext(first);
}
System.out.println("最后留着圈中的节点编号为:" + first.getNo());
}
完整代码
/**
* @author trf
* @date 2019-09-06 19:49
*/
public class Josephu {
public static void main(String[] args) {
CircleSingleLinkedList cll = new CircleSingleLinkedList();
cll.addBoy(5);
cll.josephu(1, 2, 5);
}
}
//创建一个环形的单向链表
class CircleSingleLinkedList {
//创建一个first节点,当前没有编号
private Boy first = null;//第一个节点
/**
* 根据用户的输入,计算出圈的顺序
*
* @param startNo 表示从第几个小孩开始数
* @param countNum 表示数几次
* @param nums 表示最初圈中有多少小孩
*/
public void josephu(int startNo, int countNum, int nums) {
if (first == null || startNo < 1 || startNo > nums) {
System.out.println("参数输入有误,请重新输入");
return;
}
Boy helper = first;
//让helpBoy指向最后一个节点
while (helper.getNext() != first) {//下一个节点是first节点时停止移动
helper = helper.getNext();//当前helpBoy前移
}
//报数前,先让first移动到开始报数的节点,helpBoy到开始报数的前一个节点,startNo-1次
for (int i = 0; i < startNo - 1; i++) {
helper = helper.getNext();
first = first.getNext();
}
//开始报数出圈操作
while (first != helper) {//圈中只有一个人
//移动first到出圈的节点,移动helper出圈的节点的前一个节点
for (int i = 0; i < countNum - 1; i++) {
helper = helper.getNext();
first = first.getNext();
}
//first指向的就是要出圈的节点
System.out.println("出圈节点的编号为:" + first.getNo());
//出圈操作
first = first.getNext();
helper.setNext(first);
}
System.out.println("最后留着圈中的节点编号为:" + first.getNo());
}
/**
* 添加小孩节点,构建成一个环形链表
*/
public void addBoy(int nums) {
//nums做数据校验
if (nums < 1) {
System.out.println("nums的值不正确");
return;
}
Boy curBoy = null;//辅助指针,帮助构建环形链表
for (int i = 1; i <= nums; i++) {
//根据编号,创建小孩节点
Boy boy = new Boy(i);
//添加新节点
//如果是第一个节点
if (1 == i) {
first = boy;
first.setNext(first);//指向自己,构成环状
curBoy = first;//让curBoy指向第一个
} else {
curBoy.setNext(boy);
curBoy = boy;//让curBoy指向新添加的节点
curBoy.setNext(first);//新添加的节点next指向first节点
}
}
}
public void showBoy() {
if (null == first) {
System.out.println("链表为空");
return;
}
Boy curBoy = first;//辅助指针,帮助遍历
do {//先执行一次再判断
System.out.println("节点的编号:" + curBoy.getNo());
curBoy = curBoy.getNext();//当前节点前移
} while (curBoy != first);//下一个节点是first节点时停止遍历
}
}
//创建一个Boy类,表示一个节点
class Boy {
private int no;//编号
private Boy next;//指向下一个节点,默认null
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;
}
}