0
点赞
收藏
分享

微信扫一扫

链表——约瑟夫问题

芝婵 2022-01-17 阅读 74

代码实现

import java.util.Arrays;
public class test{

	private static class Node<T>{
		//存储数据
		T item;
		//下一个结点
		Node next;
		public Node(T item,Node next)
		{
			this.item=item;
			this.next=next;
		}	
	}
	
public static void main(String[] args)
{
	//1.构建循环链表,41个结点分别存储1~41之间的值
	//用来记录首结点
	Node<Integer>first=null;
	//用来记录前一个结点 
	Node<Integer>pre=null;
	
	for(int i=1;i<=41;i++)
	{
		//如果是第一个结点
		if(i==1) {
			first=new Node(i,null);
			pre=first;
			continue;
		}
		
		//如果不是第一个结点
		Node<Integer> newNode=new Node<>(i,null);
		pre.next=newNode;
		pre=newNode;
		//如果是最后一个结点,将其下一个结点变为first——循环链表
		if(i==41)
		{
			pre.next=first;
		}		
	}
	//2.count报数
 int count=0;
	//3.遍历循环链表,
	//记录每次遍历拿到的结点,默认从首结点开始
 Node<Integer> n=first;
 //记录当前结点的上一个结点
 Node<Integer> before=null;
 while(n!=n.next)
 {
	 //模拟报数
	 count++;
	 //判断当前报数是不是为3,若为3,删除该结点并且打印出该结点,重置count。让当前结点n后移
	 if(count==3)
	 {
		 before.next=n.next;
		 System.out.print(n.item+",");
		 count=0;
		 n=n.next;
	 }
	//如果不是3,让before变为当前结点,当前结点后移
	 else
	 {
		before=n;
		n=n.next;
	 } 
 }
 //打印最后一个元素
 System.out.println(n.item);
	
}

}

举报

相关推荐

0 条评论