# java循环链表实现约瑟夫环问题

<pre name="code" class="java">package com.marthevin.linkedlist;

import java.util.Scanner;

public class YuesefuCircleTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Input a num to create a circle....");
Scanner  scanner = new Scanner(System.in);
int N = scanner.nextInt();
System.out.println("Input a num to count...");
int M = scanner.nextInt();
int[] array = new int[N];
for(int i=1;i<=N;i++)
{
array[i-1] =i;
}

YuesefuCircle circle = new YuesefuCircle();
circle.createCircleByArr(array);
circle.deleteNodeByDivident(M);
circle.printCircle();

}

}
class NodeC{
NodeC next;
int data;
public NodeC(int data)
{
this.data = data;
this.next = null;
}
}

class YuesefuCircle{
int count = 0;  //count控制最终的循环是否继续
int divident = 0; //表示输入几个数之后删除节点

public NodeC deleteNodeByDivident(int div)
{
this.divident = div;
while(this.count>1)
{
for(int i=1;i<div;i++)
{
prior = current;
current = current.next;
}
//删除current
{
}else if(current==tail) //current为tail 删除tail
{
tail = prior;
}else{
prior.next = current.next;
current = prior.next;
}
count--;
}
}

public int createCircleByArr(int[] arr)
{
int number = 0;
int length = arr.length;
for(int i=1;i<=length;i++)
{
NodeC newnode = new NodeC(arr[i-1]);
if(i == 1)
{
tail = newnode;
}else{
tail.next = newnode;
tail = newnode;
}
number++;
count++;
}
return number;
}

public void printCircle()
{
{
System.out.println("null list...");
}
}

public YuesefuCircle()
{
}

}

#### java链表实现约瑟夫环

2016-01-26 14:54:54

#### 用java实现链表并解决约瑟夫环问题

2013-08-05 10:38:48

#### 约瑟夫环 java实现

2014-04-30 09:27:43

#### 面试题—Java递归实现约瑟夫环

2015-04-18 23:33:40

#### Java实现约瑟夫环问题

2017-03-05 14:48:34

#### 约瑟夫环问题 java代码实现（高效率）

2015-06-13 19:27:55

#### Java 数组实现约瑟夫环

2014-10-26 10:07:30

#### java实现约瑟夫环

2010-09-03 15:15:00

#### java解决约瑟夫环的两种方法

2018-03-30 18:57:56

#### Java实现约瑟夫环

2015-10-16 07:08:34