2025-01-12 约瑟夫问题(环形链表)
This commit is contained in:
parent
7bc1fdd338
commit
be0ea5b1ea
136
src/dataANDcalc/java/LinkedList/CircleLinkedList.java
Normal file
136
src/dataANDcalc/java/LinkedList/CircleLinkedList.java
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
package LinkedList;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class CircleLinkedList {
|
||||||
|
|
||||||
|
private JosephNode first;
|
||||||
|
|
||||||
|
private JosephNode current;
|
||||||
|
|
||||||
|
private List<String> outList = new ArrayList<>();
|
||||||
|
|
||||||
|
public void add(JosephNode node){
|
||||||
|
if (first == null){
|
||||||
|
first = node;
|
||||||
|
first.next = first;
|
||||||
|
current = first;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
current.next = node;
|
||||||
|
node.next = first;
|
||||||
|
current = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void show(){
|
||||||
|
JosephNode temp = first;
|
||||||
|
if (first == null){
|
||||||
|
System.out.println("该环形链表不存在节点");
|
||||||
|
}
|
||||||
|
while (true){
|
||||||
|
System.out.println(temp.toString());
|
||||||
|
if (temp.next == first){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
temp = temp.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void joseph(int m){
|
||||||
|
JosephNode temp = first;
|
||||||
|
int count = 1;
|
||||||
|
while (true) {
|
||||||
|
if (temp.next == temp){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
count ++;
|
||||||
|
if (count == m){
|
||||||
|
outList.add(temp.next.name);
|
||||||
|
temp.next = temp.next.next;
|
||||||
|
temp = temp.next;
|
||||||
|
count = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.print("出队顺序:");
|
||||||
|
for(String name : outList){
|
||||||
|
System.out.print(name + ";");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
CircleLinkedList circleLinkedList = new CircleLinkedList();
|
||||||
|
Scanner scan = new Scanner(System.in);
|
||||||
|
while (true) {
|
||||||
|
System.out.println("a添加节点, s查看节点, j约瑟夫队列 exit退出:");
|
||||||
|
String handle = scan.next();
|
||||||
|
if ("exit".equals(handle)){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if ("a".equals(handle)){
|
||||||
|
System.out.println("请输入节点编号:");
|
||||||
|
int no = scan.nextInt();
|
||||||
|
System.out.println("请输入节点名称:");
|
||||||
|
String name = scan.next();
|
||||||
|
CircleLinkedList.JosephNode node = new CircleLinkedList.JosephNode(no,name);
|
||||||
|
circleLinkedList.add(node);
|
||||||
|
}
|
||||||
|
if ("s".equals(handle)){
|
||||||
|
circleLinkedList.show();
|
||||||
|
}
|
||||||
|
if ("j".equals(handle)){
|
||||||
|
System.out.println("请输入出队报数数字:");
|
||||||
|
int m = scan.nextInt();
|
||||||
|
circleLinkedList.joseph(m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scan.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class JosephNode{
|
||||||
|
private int nodeId;
|
||||||
|
private String name;
|
||||||
|
private JosephNode next;
|
||||||
|
|
||||||
|
public JosephNode(int nodeId, String name) {
|
||||||
|
this.nodeId = nodeId;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNodeId() {
|
||||||
|
return nodeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNodeId(int nodeId) {
|
||||||
|
this.nodeId = nodeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JosephNode getNext() {
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNext(JosephNode next) {
|
||||||
|
this.next = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "JosephNode{" +
|
||||||
|
"nodeId=" + nodeId +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
", next=" + next.nodeId + ":" + next.name +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user