约瑟夫问题(环状链表)详细笔记

Josephu问题为:设编号为1,2,…n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。
在这里插入图片描述

约瑟夫问题–创建环形链表思路图解:

在这里插入图片描述
核心代码:遍历小孩节点的时候,自己有写另外一种方法,但是老是缺少最后一个节点,仔细揣摩之后,发现当while条件满足时,不会打印最后一个节点

		  while(curBoy.getNext() != first) {
		  		System.out.printf("小孩的编号是%d\n",curBoy.getNo()); 
		  		curBoy = curBoy.getNext();
		   }		 


//创建一个环形的单向链表
class CircleSingleLinkedList {
	// 创建一个first节点,当前无编号
	private Boy first = null;

	// 添加小孩构建成环形链表
	public void addBoy(int num) {
		// 数据校验
		if (num < 1) {
			System.out.println("值不正确");
			return;
		}
		Boy curBoy = null;// 辅助指针,帮助构建环形链表
		// 使用for来创建环形链表
		for (int i = 1; i <= num; i++) {
			// 根据编号,创建小孩节点
			Boy boy = new Boy(i);
			// 如果是第一个小孩
			if (i == 1) {
				first = boy;
				first.setNext(first);// 构成一个环
				curBoy = first;// 让curBoy指向第一个小孩
			} else {
				curBoy.setNext(boy);// 添加新的小孩到链表里
				boy.setNext(first);// 使链表变成一个环
				curBoy = boy;// 后移,curBoy是指的是当前添加小孩的上一个小孩,添加好之后就变成了该小孩

			}
		}
	}

	// 遍历当前的环形链表
	public void showBoy() {
		// 判断链表是否为空
		if (first == null) {
			System.out.println("链表为空");
			return;
		}
		// 因为first不能动,因此使用辅助指针
		Boy curBoy = first;
		while (true) {

			System.out.printf("小孩的编号是%d\n", curBoy.getNo());
			if (curBoy.getNext() == first) {
				break;
			}
			curBoy = curBoy.getNext();
		}
		/*
		 * while(curBoy.getNext() != first) {
		 * System.out.printf("小孩的编号是%d\n",curBoy.getNo()); 
		 * curBoy = curBoy.getNext(); }
		 */
	}

约瑟夫问题–小孩出圈思路图解:

在这里插入图片描述
代码:注意helper是指向最后一个节点的,first是第一个节点,他们是同时移动的

			//将first指向的小孩节点出圈,这里用的是set方法
			first = first.getNext();
			helper.setNext(first);//这句代码很神奇

	// 根据用户的输入,计算出出圈的顺序
	/*
	 * startNo 表示从第几个小孩开始数数
	 *  countNum 表示数几下 
	 * nums 表示有几个小孩在圈中
	 */
	public void countBoy(int startNo, int countNum, int nums) {
		// 对数据进行校验
		if (first == null || startNo < 1 || startNo > nums) {
			System.out.println("参数输入有误,请重新输");
			return;
		}
		// 创建一个辅助指针,帮助小孩出圈
		Boy helper = first;
		// 让helper指向最后一个节点
		while (true) {
			if (helper.getNext() == first) {
				break;
			}
			helper = helper.getNext();
		}
		//小孩报数前,先让first和helper移动k-1次,因为开始报数的位置不一定是第一个小孩
		for(int j= 0;j<startNo -1 ;j++) {
			first = first.getNext();
			helper = helper.getNext();
		}
		//当小孩报数时,让first和helper 指针同时的移动m -1 次,然后出圈
		//这里是一个循环操作,直到圈中只有一个节点
		while(true) {
			if(helper == first) {//说明圈中只有一个节点
				break;
			}
			//让first和helper指针同时移动coutNum - 1
			for(int j= 0;j < countNum-1;j++) {
				first = first.getNext();
				helper = helper.getNext();
			}
			//这时first指向的节点,就是要出圈的小孩节点
			System.out.printf("小孩%d出圈\n",first.getNo());
			//这时将first指向的小孩节点出圈
			first = first.getNext();
			helper.setNext(first);//这句代码很神奇
		}
		System.out.printf("最后留在圈中小孩编号%d\n",first.getNo());
	}
}

//创建一个Boy,表示一个节点
class Boy {
	private int no;// 编号
	private Boy next;// 指向下一个节点

	public Boy(int no) {
		this.no = no;

	}

	@Override
	public String toString() {
		return "Boy [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;

	}

}


全部代码:

`
package com.suanfa;

public class Josephu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 测试构建和测试
		CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
		circleSingleLinkedList.addBoy(5);// 加入五个小孩节点
		//circleSingleLinkedList.showBoy();
		circleSingleLinkedList.countBoy(1, 2, 5);

	}

}

//创建一个环形的单向链表
class CircleSingleLinkedList {
	// 创建一个first节点,当前无编号
	private Boy first = null;

	// 添加小孩构建成环形链表
	public void addBoy(int num) {
		// 数据校验
		if (num < 1) {
			System.out.println("值不正确");
			return;
		}
		Boy curBoy = null;// 辅助指针,帮助构建环形链表
		// 使用for来创建环形链表
		for (int i = 1; i <= num; i++) {
			// 根据编号,创建小孩节点
			Boy boy = new Boy(i);
			// 如果是第一个小孩
			if (i == 1) {
				first = boy;
				first.setNext(first);// 构成一个环
				curBoy = first;// 让curBoy指向第一个小孩
			} else {
				curBoy.setNext(boy);// 添加新的小孩到链表里
				boy.setNext(first);// 使链表变成一个环
				curBoy = boy;// 后移,curBoy是指的是当前添加小孩的上一个小孩,添加好之后就变成了该小孩

			}
		}
	}

	// 遍历当前的环形链表
	public void showBoy() {
		// 判断链表是否为空
		if (first == null) {
			System.out.println("链表为空");
			return;
		}
		// 因为first不能动,因此使用辅助指针
		Boy curBoy = first;
		while (true) {

			System.out.printf("小孩的编号是%d\n", curBoy.getNo());
			if (curBoy.getNext() == first) {
				break;
			}
			curBoy = curBoy.getNext();
		}
		/*
		 * while(curBoy.getNext() != first) {
		 * System.out.printf("小孩的编号是%d\n",curBoy.getNo()); curBoy = curBoy.getNext(); }
		 */
	}

	// 根据用户的输入,计算出出圈的顺序
	/*
	 * startNo 表示从第几个小孩开始数数 countNum 表示数几下 nums 表示有几个小孩在圈中
	 */
	public void countBoy(int startNo, int countNum, int nums) {
		// 对数据进行校验
		if (first == null || startNo < 1 || startNo > nums) {
			System.out.println("参数输入有误,请重新输");
			return;
		}
		// 创建一个辅助指针,帮助小孩出圈
		Boy helper = first;
		// 让helper指向最后一个节点
		while (true) {
			if (helper.getNext() == first) {
				break;
			}
			helper = helper.getNext();
		}
		//小孩报数前,先让first和helper移动k-1次,因为开始报数的位置不一定是第一个小孩
		for(int j= 0;j<startNo -1 ;j++) {
			first = first.getNext();
			helper = helper.getNext();
		}
		//当小孩报数时,让first和helper 指针同时的移动m -1 次,然后出圈
		//这里是一个循环操作,直到圈中只有一个节点
		while(true) {
			if(helper == first) {//说明圈中只有一个节点
				break;
			}
			//让first和helper指针同时移动coutNum - 1
			for(int j= 0;j < countNum-1;j++) {
				first = first.getNext();
				helper = helper.getNext();
			}
			//这时first指向的节点,就是要出圈的小孩节点
			System.out.printf("小孩%d出圈\n",first.getNo());
			//这时将first指向的小孩节点出圈
			first = first.getNext();
			helper.setNext(first);//
		}
		System.out.printf("最后留在圈中小孩编号%d\n",first.getNo());
	}
}

//创建一个Boy,表示一个节点
class Boy {
	private int no;// 编号
	private Boy next;// 指向下一个节点

	public Boy(int no) {
		this.no = no;

	}

	@Override
	public String toString() {
		return "Boy [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;

	}

}
`
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你在狗叫什么、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值