利用环形链表解决约瑟夫环问题

用环形链表解决约瑟夫环的问题 设编号为1,2,3,...,num的num个人坐成一圈,约定编号为start(1<=start<=num)的人从1开始报数,数到count的那个人出列,它的下一位又开始从1报数,数到count的人又出列,以此类推,直到所有人出列位置,由此 产生一个出队编号的序列
假设有5个人(num=5),编号为1的人从1开始报数(start=1),数到5的人出列(count=2)。
首先,写一个实体类用来表示人。
class Child {
	private int no;
	private Child next;

	public Child(int no) {
		super();
		this.no = no;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public Child getNext() {
		return next;
	}

	public void setNext(Child next) {
		this.next = next;
	}
}

再写一个环形链表,这里只需要写添加和遍历方法以及一个出圈的方法

class CircleLinkedList {
	// 创建一个节点没有编号
	private Child first = null;

	/**
	 * 
	 * @param num 表示小孩的个数
	 */
	public void addChild(int num) {
		if (num < 1) {
			System.out.println("小孩的个数必须大于1");
			return;
		}
		Child temp = null;

		for (int i = 1; i <= num; i++) {
			Child child = new Child(i);
			if (i == 1) {
				first = child;
				child.setNext(first);
			} else {
				temp.setNext(child);
				child.setNext(first);
			}
			temp = child;
		}
	}

	// 遍历环形链表
	public void show() {
		if (first == null) {
			System.out.println("环形链表为空");
			return;
		}
		Child temp = first;
		while (true) {
			System.out.println("小孩的编号是" + temp.getNo());
			temp = temp.getNext();
			if (temp == first) {
				break;
			}
		}
	}

	/**
	 * 
	 * @param start 开始报数的小孩的编号
	 * @param count 报到count出圈
	 * @param num   小孩的个数
	 */
	public void outCircle(int start, int count, int num) {
		if (first == null || start < 1 || start > num) {
			System.out.println("环形链表为空或者出圈参数设置不合理");
			return;
		}

		Child temp = first; // 辅助指针,这个辅助指针在这里的作用永远都是代表着环形链表的最后一个节点
		// 先将辅助指针移动到链表的最尾端
		while (true) {
			if (temp.getNext() == first) {
				break;
			}
			temp = temp.getNext();
		}

		// 然后将头节点指向到开始报数的小孩
		// 将辅助指针指向开始报数小孩的前一个小孩
		// 例如3号小孩开始报数,first应该指向三号小孩,辅助指针指向2号小孩
		for (int i = 0; i < start - 1; i++) {
			first = first.getNext();
			temp = temp.getNext();
		}
		// 开始退圈游戏
		while (true) {
			// 表示循环队列是否为空的条件,当头节点和尾节点重合时,链表为空
			if (temp == first) {
				break;
			}
			
			//报道count出圈,指针同时移动count-1次
			for (int i = 0; i < count - 1; i++) {
				first = first.getNext();
				temp = temp.getNext();
			}
			System.out.println("出圈小孩的编号是"+first.getNo());
			//小孩出圈就是将first指向的这个节点删除
			//将头节点指向下一个节点
			//辅助指针将头节点和尾节点连接
			first = first.getNext();//first指向下一个节点
			temp.setNext(first);//将尾部节点和头节点连接
		}
		System.out.println("最后留在圈中的小孩是"+first.getNo());
	}

}

然后写一个主方法进行测试,linkedList.outCircle(1, 2, 5)第一个参数是开始报数的小孩的编号,第二个参数是报到几出圈,第三个参数是环形链表的节点个数,也就是小孩的人数

public class CircleLinkedListDemo {
	public static void main(String[] args) {
		CircleLinkedList linkedList = new CircleLinkedList();
		linkedList.addChild(5);
		linkedList.show();
		linkedList.outCircle(1, 2, 5);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值