Java数据结构-环形链表(约瑟夫环)

环形链表(约瑟夫环)-建立及出圈操作

package A_1;

public class cirlinkeddemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*问题描述:
		 * 1.本次代码仅完成环形链表的创建及遍历问题
		 * 2.场景为小孩丢手绢问题
		 * 3.约瑟夫问题(约瑟夫环)
		 * */
		
		//演示
		cirlinked cir = new cirlinked();//创建环形链表
		cir.addcirlinked(5);//加入25个小孩节点
		//显示运行结果
		cir.listcirlinked();	
		
		//出圈演示
		cir.outcirlinked(5,3,3);
	}

}
//模块二:创建环形链表并定义一系列操作
class cirlinked{
	//first节点须存在,先定义
	private Boy first = new Boy(0);
	//开始创建环形链表
	public void addcirlinked(int nums) {
		Boy curBoy = first;//定义辅助指针,指向新加入节点
		//定义加入环形链表的个数,若少于1则人文添加节点到环形链表无意义
		if(nums<=0) {
			System.out.printf("不能创建 %d 个节点的链表\n", nums);
			return;
		}
		//创建的节点个数符合要求
		for(int i = 0;i<=nums;i++) {
		//将第一个节点赋给first
			Boy boy = new Boy(i);
			if(i==1) {//只有一个小孩时
				first = boy;
				first.setNext(first);//一个节点时指向自己
				curBoy = first;//辅助指针指向自己
			}else {//大于一个节点时
				curBoy.setNext(boy);//连接新节点
				boy.setNext(first);//新节点连接到第一个节点
				curBoy = boy;		//辅助指针移动到新节点为下次插入做准备
			}
		}
	
	}
	
	public void listcirlinked() {
		if(first.getNo()==0) {
			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();//后移
		}
		
	}
	
	//定义出圈方法
	/**
	 * Alt+Shift+J
	 * @param num	小孩个数(节点数)	5
	 * @param startno	开始报数的节点	3
	 * @param length	报数走过的步长	3
	 * 预计出圈顺序5->3->2->4->1
	 */
	public void outcirlinked(int num,int startno,int length) {
		//当输入数据不合法时报错
		if(num<=0 || startno>num || length<=0 || startno<1 || first==null) {
			System.out.println("输入数据不合法,检查后输入");
		}
		//当输入数据合法时定义指针指向预定位置(first节点的前一个位置)
		Boy helper = first;
		while(true) {
			if(helper.getNext()==first) {
				break;
			}
			helper = helper.getNext();
		}
		//辅助指针已经就位(在first节点之前)
		for(int i = 0;i<startno-1;i++) {
			first = first.getNext();
			helper = helper.getNext();
		}
		//first指向预定节点:开始出圈
		while(true) {
			if(helper == first) {//只有一个节点时
				break;
			}
			//指针根据开始点信息就位(从第三个节点移动至第五个节点)
			for(int j=0;j<startno-1;j++) {
				first = first.getNext();
				helper = helper.getNext();
			}
			//移动到开始出圈节点
			System.out.printf("小孩节点%d 出圈\n",first.getNo());
			first = first.getNext();//移动到下一节点
			helper.setNext(first);
		}
		//跳出时仅有一个节点
		System.out.printf("最后的小孩节点为%d\n",first.getNo());
	}
	
}

//模块一:创建小孩节点类型
class Boy{
	
	//创建小孩节点私有变量
	private int no;
	private Boy next;
	
	public Boy(int no) {
		this.no = no;
	}
	//创建方法导出私有变量
	public int getNo() {		//节点内属性,no;外部可调用
		return no;
	}
	public void setNo(int no) {	//外部可修改
		this.no = no;
	}
	public Boy getNext() {//节点属性
		return next;
	}
	public void setNext(Boy next) {//节点内指向,需要参数 
		this.next = next;
	}
	
}

演示结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值