这是我写的
package com.longer.link;
public class CircleLinkListDemo {
public static void main(String[] args) {
CircleLinkList circleLinkList=new CircleLinkList();
for (int i = 1; i <=5 ; i++) {
CircleNode circleNode=new CircleNode(i,"同学"+i,"a"+i);
circleLinkList.add(circleNode);
}
// circleLinkList.show();
circleLinkList.countNode(1,2);
}
}
class CircleNode{
private int no;
private String name;
private String nickeName;
private CircleNode next;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickeName() {
return nickeName;
}
public void setNickeName(String nickeName) {
this.nickeName = nickeName;
}
public CircleNode getNext() {
return next;
}
public void setNext(CircleNode next) {
this.next = next;
}
public CircleNode(int no, String name, String nickeName) {
this.no = no;
this.name = name;
this.nickeName = nickeName;
}
}
class CircleLinkList{
private CircleNode first=new CircleNode(-1,"","");
private CircleNode rear=first;
public void add(CircleNode node){
if(first.getNext()==null){
first=node;
}
while (node.getNext()!=first){
rear.setNext(node);
node.setNext(first);
rear=node;
}
}
public void show(){
CircleNode temp=first;
while (true){
System.out.println(temp.getName());
temp=temp.getNext();
if(temp==first){
break;
}
}
}
public void countNode(int startNo,int countNums){
if(startNo<1||countNums<1){
System.out.println("格式不对");
}
//偏移
for (int i = 1; i <startNo ; i++) {
first=first.getNext();
}
//数次
int count=0;
while (true){
//获取要抛出的上个节点
for (int i = 1; i < countNums-1; i++) {
first=first.getNext();
}
if(first==first.getNext()&&count++==1){
break;
}
System.out.println(first.getNext().getName());
first.setNext(first.getNext().getNext());
first=first.getNext();
}
}
}
这是课程写的
package com.longer.link;
public class Josephu {
public static void main(String[] args) {
//实例化一个单链表对象
CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
circleSingleLinkedList.addBoy(1);// 加入5个小孩节点
// 测试一把小孩出圈是否正确
System.out.println("小孩出圈顺序为:");
circleSingleLinkedList.countBoy(1, 2, 5);// 2->4->1->5->3
}
}
//创建一个环形的单向链表
class CircleSingleLinkedList {
// 创建一个first节点,当前没有编号
private Boy first = null;
// 添加小孩节点,构建成一个环形的链表
public void addBoy(int nums) {// nums为添加的小孩节点的个数
// 对nums做一个简单的数值检验
if (nums < 1) {
System.out.println("nums的值不正确!");
return;
}
Boy curBoy = null;// 辅助指针(变量),帮助构建环形链表
// 使用for循环创建我们的环形链表
for (int i = 1; i <= nums; i++) {
// 根据编号创建小孩节点
Boy boy = new Boy(i);
// 如果是第一个小孩
if (i == 1) {
first = boy;
first.setNext(first);// 构成一个环,只有一个小孩
curBoy = first;// 因为first不能动,所以让curBoy指向第一个小孩,帮助我们构建环形链表
} else {
curBoy.setNext(boy);//
boy.setNext(first);//
curBoy = boy;//
}
}
}
// 遍历当前这个环形链表
public void showBoy() {
// 判断链表是否为空
if (first == null) {
System.out.println("链表为空,没有任何小孩");
return;
}
// 因为first不能动,所以我们仍然使用辅助指针帮助我们完成遍历
Boy curBoy = first;// 让curBoy指向第一个小孩
while (true) {
System.out.printf("小孩的编号%d \n", curBoy.getNo());
if (curBoy.getNext() == first) {// 说明已经遍历完毕
break;
}
curBoy = curBoy.getNext(); // curBoy指针后移
}
}
// 根据用户的输入,计算小孩出圈的顺序:
/**
* @param startNo表示从第几个小孩子开始数
* @param countNum表示数几下
* @param 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) {// 说明helper指向了最后一个小孩
break;
}
helper = helper.getNext();// 让helper继续往下走
}
// 小孩报数前让helper和first向前移动startNo-1次,这样就使得这两个指针变量移动到小孩准备开始报数时的位置
for (int j = 0; j < startNo - 1; j++) {
first = first.getNext();
helper = helper.getNext();
}
// 当小孩报数时,让first和helper移动countNum-1次,然后出圈
// 这里是一个循环的操作,知道圈中只有一个小孩节点
while (true) {
if (helper == first) {// 满足这个条件说明圈中只有一个人
break;
}
// 让first和helper移动countNum-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();// 将first后移
helper.setNext(first);// 将helper的下一个节点指向first
}
System.out.printf("最后留在圈中的小孩编号%d \n", first.getNo());
}
}
//创建一个Boy类,表示一个节点
class Boy {
private int no;// 编号
private Boy next;// 指向下一个节点,默认为空
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;
}
}