- package com.heima.test;
- import com.heima.bean.Child;
- public class Test08_Linked {
- public static Child lastChild;
- public static Child first;
-
- public static void main(String[] args) {
- addTo(8);
- //showChild();
- Child c = deleteChild();
- System.out.println("最后剩下的小孩是" + c);
- }
-
- public static Child deleteChild() {
- Child c = first;
- for (int i = 1; c.getNext() != c; c = c.getNext(), i++) {
- if (i % 3 == 0) {
- remove(c);
- System.out.println(c + "离开了我们。");
- }
- }
- System.out.println("最后留下来的是" + c);
- return(c);
-
-
- }
-
- public static void showChild() {
- Child c = first;
- while (true) {
- System.out.print(c);
- c = c.getNext();
- if(c == first) {
- break;
- }
-
- }
- }
-
- public static void addTo(int num) {
- for (int i = 0; i < num; i++) {
- Child c = new Child("小孩", i + 1);
- // System.out.println(c);
- if (i == 0) {
- first = c;
- } else if (i == num - 1) {
- lastChild.setNext(c);
- first.setPrevious(c);
- c.setNext(first);
- c.setPrevious(lastChild);
-
- // System.out.println(c.getNext());
- // System.out.println(c.getPrevious());
- // System.out.println(c);
- } else {
- lastChild.setNext(c);
- c.setPrevious(lastChild);
- /* System.out.println("----------");
- System.out.println(c);
- System.out.println(c.getNext());
- System.out.println(c.getPrevious());*/
- }
- lastChild = c;
- }
- }
-
- public static void remove(Child c) {
- c.getPrevious().setNext(c.getNext());
- c.getNext().setPrevious(c.getPrevious());
- }
- }
复制代码
|
|