Java实现不带头结点的单向循环链表(解决约瑟夫环问题)



一、什么是单向循环链表?

如果把单链表的最后一个节点的指针指向链表头部,而不是指向NULL,那么就构成了一个单向循环链表,通俗讲就是把尾节点的下一跳指向头结点。
在这里插入图片描述



二、约瑟夫环问题

设有n个人围坐在一张圆桌周围,先从某个人开始从1报数,数到m的人出列(即离开座位,不参与以后的报数),然后从出列的下一个人开始重新从1报数,数到m的人又出列,如此下去直到所有人都出列为止,试求出它们的出列顺序。

例如:当n=8,m=4时,若从第一个人(每个人的编号依次为1,2,…,n)开始报数,则得到的出列次序为:4,8,5,2,1,3,7,6;



三、约瑟夫环问题的解决

用不带头结点的单向循环链表的实现来解决约瑟夫环问题:

1、结点的定义:

class Node1{
	public int num;			//编号
	public int data;		//存放结点值
	public Node1 next;		//指向下一个的节点
	public Node1(int n,int d) {	//带两个参数时的构造函数
		num = n;
		data = d;
	}
}

2、创建不带头结点的单向循环链表:

class LinkList1{
	public Node1 head;	//头结点
	public Node1 tail;	//尾节点
	public int count=0;	//记录元素个数
	Node1 temp = null;	//用来改变指针节点位置
	public LinkList1() {		//无参构造函数
		head = null;	//初始化表
		tail = null;
		count = 0;		//元素个数清零
	}
}

3、用尾插法插入元素:

//在尾部插入节点
public void insert(Node1 t) {
	if(count==0) {		//如果是空表,则头尾节点相等
		head=t;
		tail=t;
	}
	else {
		tail.next = t;	//将尾节点连接到t
		t.next = head;	//将t连接到头节点
		tail = t;		//然后将尾节点更新为t
	}
	count++;	//元素个数加1
}

4、删除并返回指定元素:

//出列函数,节点t后第i位元素出列
public int exit(Node1 t,int i) {
	int sum=1;	//第一个节点初始化为1
	while(sum<i) {		//移动i-1次,找到要出列元素的节点
		t = t.next;		//移动到下一个节点
		sum++;
	}
	int n = t.next.num;	//要出列的人的编号
	if(count!=1) {
		t.next=t.next.next;	//直接将当前指针连接到下下个点的指针
		t=t.next;	//移动到下个节点
	}
	else {		//如果移动到了头部就清空头部尾部结点
		t=null;
		head=null;
		tail=null;
	}
	count--;	//人数减1
	temp=t;	//将当前的指针传出去,不然不能改变指针所在位置
	return n;	//返回出列的人的编号
}

5、判断链表是否为空:

//判断循环链表是否为空
public boolean isEmpty() {
	return count==0;	//元素个数为0返回1,否则返回0
}

6、约瑟夫环问题的解决:

public void Josephus(int n,int m) {
	for(int i=1;i<=n;i++) 	//初始化链表
		this.insert(new Node1(i,0));
	Node1 t=head;	//将指针初始化为头结点
	while(!this.isEmpty()) {
		int i = this.exit(t,m-1);	//只需移动m-1次
		t=temp;
		System.out.print(i + " ");
	}
}


附上全部代码:

import java.util.Scanner;

class Node1{
	public int num;			//编号
	public int data;		//存放结点值
	public Node1 next;		//指向下一个的节点
	public Node1(int n,int d) {	//带两个参数时的构造函数
		num = n;
		data = d;
	}
}

class LinkList1{
	public Node1 head;	//头结点
	public Node1 tail;	//尾节点
	public int count=0;	//记录元素个数
	Node1 temp = null;	//用来改变指针节点位置
	public LinkList1() {		//无参构造函数
		head = null;	//初始化表
		tail = null;
		count = 0;		//元素个数清零
	}
	
	//在尾部插入节点
	public void insert(Node1 t) {
		if(count==0) {		//如果是空表,则头尾节点相等
			head=t;
			tail=t;
		}
		else {
			tail.next = t;	//将尾节点连接到t
			t.next = head;	//将t连接到头节点
			tail = t;		//然后将尾节点更新为t
		}
		count++;	//元素个数加1
	}
	
	//出列函数,节点t后第i位元素出列
	public int exit(Node1 t,int i) {
		int sum=1;	//第一个节点初始化为1
		while(sum<i) {		//移动i-1次,找到要出列元素的节点
			t = t.next;		//移动到下一个节点
			sum++;
		}
		int n = t.next.num;	//要出列的人的编号
		if(count!=1) {
			t.next=t.next.next;	//直接将当前指针连接到下下个点的指针
			t=t.next;	//移动到下个节点
		}
		else {		//如果移动到了头部就清空头部尾部结点
			t=null;
			head=null;
			tail=null;
		}
		count--;	//人数减1
		temp=t;	//将当前的指针传出去,不然不能改变指针所在位置
		return n;	//返回出列的人的编号
	}
	
	//判断循环链表是否为空
	public boolean isEmpty() {
		return count==0;	//元素个数为0返回1,否则返回0
	}
	
	//约瑟夫环问题
	public void Josephus(int n,int m) {
		for(int i=1;i<=n;i++) 	//初始化链表
			this.insert(new Node1(i,0));
		Node1 t=head;	//将指针初始化为头结点
		while(!this.isEmpty()) {
			int i = this.exit(t,m-1);	//只需移动m-1次
			t=temp;
			System.out.print(i + " ");
		}
	}
}
public class Task_1_3 {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		System.out.print("请输入圈中人数:");
		int n = cin.nextInt();	//共有n个人
		System.out.print("请输入密码:");
		int m = cin.nextInt();	//密码m
		LinkList1 L = new LinkList1();
		System.out.print("出圈人编号依次为:");
		L.Josephus(n, m);
	}
}

运行结果:
在这里插入图片描述

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值