package com.structure.demo; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class JosephActivity extends Activity { @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_joseph); CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList(); circleSingleLinkedList.addBoy(3); circleSingleLinkedList.show(); } } class CircleSingleLinkedList{ Boy first = null; Boy curBoy = null; public void addBoy(int nums){ if (nums<1){ Log.i("tag","nums不合法"); } for (int i =1; i <=nums ; i++) { Boy boy = new Boy(i); if (i == 1){ //当前小孩付给第一个小孩 first = boy; //第一个小孩的后一个指向第一个小孩形成链表 first.next = first; //把第一个小孩设置为当前元素 curBoy = first; }else{ //把当前小孩设置为下一个元素 curBoy.next = boy; //把第一个小孩设置为当前小孩的下一个节点 boy.next = first; //设置当前节点 curBoy = boy; } } } public void show(){ if (first == null){ Log.i("tag","链表为空"); return; } Boy curBoy = first; while (true){ Log.i("tag","当前小孩的编号为"+curBoy.no); if (curBoy.next == first){ break; } curBoy = curBoy.next; } } } class Boy{ int no; Boy next; public Boy(int no) { this.no = no; } }