public static void main(String[] args) {ArrayList arrayList = new ArrayList();Node zhangsan = new Node("张三");Node lisi = new Node("李四");Node wanger = new Node("王二");//模拟双向链表,将表中元素依次连接起来zhangsan.next = lisi;lisi.next = wanger;wanger.pre = lisi;lisi.pre = zhangsan;//加入头指针和尾指针Node first = zhangsan;Node last = wanger;//将元素加入到arryList中arrayList.add(zhangsan);arrayList.add(lisi);arrayList.add(wanger);System.out.println(arrayList);//正向打印输出while (true){if(first==null){break;}System.out.println(first);first = first.next;}System.out.println("===========");//从尾指针打印输出while (true){if(last==null){break;}System.out.println(last);last = last.pre;}//添加元素麻子,添加在李四后面Node mazi = new Node("麻子");lisi.next = mazi;mazi.next = wanger;wanger.pre = mazi;mazi.pre = lisi;//此时first已经指向最后一个元素,因此需要重置first、first = zhangsan;//正向打印输出System.out.println("=======插入后的元素=======");while (true){if(first==null){break;}System.out.println(first);first = first.next;}}
}class Node {public Object item;public Node next;public Node pre;public Node(Object name) {this.item = name;}@Overridepublic String toString() {return "Node name=" + item;}
}