Java实现简单双向链表


  1 public class TwoWayList {
  2     public static void main(String[] args) {
  3         Node blue = new Node();
  4         blue.cat = new Cat("blue",5);
  5 
  6         Node yellow = new Node();
  7         yellow.cat = new Cat("yellow",4);
  8 
  9         Node green = new Node();
 10         green.cat = new Cat("green",3);
 11 
 12         //向后指
 13         blue.next = yellow;
 14         yellow.next = green;
 15 
 16         //向前指
 17         green.pre = yellow;
 18         yellow.pre = blue;
 19 
 20         //头和尾(reset)
 21         Node first = blue;
 22         Node last = green;
 23 
 24         System.out.println("从头到尾");
 25         while (true){
 26             if (first == null){
 27                 break;
 28             }
 29             System.out.println("name:"+first.cat.name+"\tage:"+first.cat.age);
 30             first = first.next;
 31         }
 32         System.out.println("从尾到头");
 33         while (true){
 34             if (last == null){
 35                 break;
 36             }
 37             System.out.println("name:"+last.cat.name+"\tage:"+last.cat.age);
 38             last = last.pre;
 39         }
 40 
 41 
 42         //头和尾(reset)
 43         first = blue;
 44         last = green;
 45         //插入
 46         Node red = new Node();
 47         red.cat = new Cat("red",11);
 48         blue.next = red;
 49         red.next = yellow;
 50         yellow.pre = red;
 51         red.pre = blue;
 52 
 53         System.out.println("--------------------------------------");
 54         System.out.println("从头到尾");
 55         while (true){
 56             if (first == null){
 57                 break;
 58             }
 59             System.out.println("name:"+first.cat.name+"\tage:"+first.cat.age);
 60             first = first.next;
 61         }
 62         System.out.println("从尾到头");
 63         while (true){
 64             if (last == null){
 65                 break;
 66             }
 67             System.out.println("name:"+last.cat.name+"\tage:"+last.cat.age);
 68             last = last.pre;
 69         }
 70 
 71         //头和尾(reset)
 72         first = blue;
 73         last = green;
 74         //修改
 75         red.cat.name = "修改的";
 76         red.cat.age = 99;
 77         System.out.println("--------------------------------------");
 78         System.out.println("从头到尾");
 79         while (true){
 80             if (first == null){
 81                 break;
 82             }
 83             System.out.println("name:"+first.cat.name+"\tage:"+first.cat.age);
 84             first = first.next;
 85         }
 86         System.out.println("从尾到头");
 87         while (true){
 88             if (last == null){
 89                 break;
 90             }
 91             System.out.println("name:"+last.cat.name+"\tage:"+last.cat.age);
 92             last = last.pre;
 93         }
 94 
 95         //头和尾(reset)
 96         first = blue;
 97         last = green;
 98         //删除
 99         first.next = yellow;
100         yellow.pre = first;
101         System.out.println("--------------------------------------");
102         System.out.println("从头到尾");
103         while (true){
104             if (first == null){
105                 break;
106             }
107             System.out.println("name:"+first.cat.name+"\tage:"+first.cat.age);
108             first = first.next;
109         }
110         System.out.println("从尾到头");
111         while (true){
112             if (last == null){
113                 break;
114             }
115             System.out.println("name:"+last.cat.name+"\tage:"+last.cat.age);
116             last = last.pre;
117         }
118 
119     }
120 }

Node类

1 public class Node {
2     public Cat cat;
3     public Node next;
4     public Node pre;
5 }

Cat类

public class Cat {
    public String name;
    public int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

相关