package zhangs;publicclassClinked{publicstaticvoidmain(String[] args){
Linkedboy linkedboy =newLinkedboy();
linkedboy.addBoy(25);
linkedboy.show();}}classLinkedboy{public Boy first = null;publicvoidaddBoy(int number){
Boy cur = first;if(number ==0){
System.out.println("不能为0");return;}//循环创建节点,让第一个进来的节点为first节点.并让他组成环形,最后一个节点next指向第一个节点for(int i =1; i <= number+1;i++){
Boy boy =newBoy(i);if(first == null){
first = boy;
first.next = first;
cur = boy;}else{
cur.next = boy;
boy.next = first;
cur = boy;}}}publicvoidshow(){
Boy cur = first;if(first == null){
System.out.println("空链表");return;}//判断下一个boy是不是为第一个boy,如果是结束循环while(cur.next != first){
System.out.println(cur);
cur = cur.next;}}}classBoy{publicint no;public Boy next;publicBoy(int no){this.no = no;}@Overridepublic String toString(){return"boy{"+"no="+ no +'}';}}
标题约瑟夫问题单向环形链表实现
package zhangs;publicclassClinked{publicstaticvoidmain(String[] args){
Linkedboy linkedboy =newLinkedboy();
linkedboy.addBoy(25);
linkedboy.show();
linkedboy.countboy(1,2);}}classLinkedboy{public Boy first = null;publicvoidcountboy(int startnum,int number){
Boy firstP = first;
Boy temp = first;if(startnum <=0|| number <=0){
System.out.println("数据不合法");}while(true){if(temp.next == first){break;}
temp = temp.next;}for(int i =0;i < startnum-1; i++){
firstP = firstP.next;
temp = temp.next;}while(true){if(firstP.next == firstP){break;}for(int j =0; j < number-1; j++){
firstP = firstP.next;
temp = temp.next;
System.out.println("小孩出圈"+firstP);}
temp.next = firstP.next;
firstP = temp.next;}
System.out.println("最后小孩出圈"+firstP);}publicvoidaddBoy(int number){
Boy cur = first;if(number ==0){
System.out.println("不能为0");return;}//循环创建节点,让第一个进来的节点为first节点.并让他组成环形,最后一个节点next指向第一个节点for(int i =1; i <= number ;i++){
Boy boy =newBoy(i);if(first == null){
first = boy;
first.next = first;
cur = boy;}else{
cur.next = boy;
boy.next = first;
cur = boy;}}}publicvoidshow(){
Boy cur = first;if(first == null){
System.out.println("空链表");return;}//判断下一个boy是不是为第一个boy,如果是结束循环while(cur.next != first){
System.out.println(cur);
cur = cur.next;}
System.out.println(cur);}}classBoy{publicint no;public Boy next;publicBoy(int no){this.no = no;}@Overridepublic String toString(){return"boy{"+"no="+ no +'}';}}
环形单向链表实现package zhangs;public class Clinked { public static void main(String[] args) { Linkedboy linkedboy = new Linkedboy(); linkedboy.addBoy(25); linkedboy.show(); }}class Linkedboy{ public Boy first = null; p