首先创建一个node的类
class Node{
public Object item;
//真正存放内容
public Node next;
public Node pre;
public Node(Object item) {
this.item = item;
}
@Override
public String toString() {
return "Node{" +
"item=" + item +
'}';
}
main方法中:
public static void main(String[] args) {
//创建三个对象形成链表
Node a = new Node("a");
Node b = new Node("b");
Node c = new Node("c");
//next连接到下一个对象
a.next = b;
b.next = c;
//pre连接到上一个对象
c.pre = b;
b.pre = a;
//使first指向第一个对象,last指向最后一个对象
Node first = a;
Node last = c;
//遍历,from first
while (true){
if(first == null){
break;
}
System.out.println(first);
first = first.next;
}
//from last
while (true){
if(last == null){
break;
}
System.out.println(last);
last = last.pre;
}
}