日期:2014-05-20 浏览次数:20677 次
import java.util.Scanner; /** * @ClassName: Test4 * @Description: TODO(这里用一句话描述这个类的作用) * @author A18ccms a18ccms_gmail_com * @date 2012-2-3 下午12:30:38 */ class Test4 { private static class Node { public int no;// 编号 public Node next;// 下一个节点 public Node(int no) { this.no = no; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入总人数:"); int totalNum = scanner.nextInt(); System.out.print("请输入报数的大小:"); int cycleNum = scanner.nextInt(); Node header = new Node(1); Node pointer = header; for (int i = 2; i <= totalNum; i++) { //指向下一个节点 pointer.next = new Node(i); //前移 pointer = pointer.next; } //形成循环链表 尾部指向头部 pointer.next = header; // 初始化环形链表结束 System.out.println("以下是出列的顺序:"); while (pointer != pointer.next) { for (int i = 1; i < cycleNum; i++) { //从头开始 pointer = pointer.next; } System.out.println(pointer.next.no); //前进 pointer.next = pointer.next.next; } System.out.println(pointer.next.no); } }