package Collection; public class LinkedList01 { public static void main(String[] args) { //模拟简单的双向链表 Node jack = new Node("jack"); Node tom = new Node("tom"); Node mark = new Node("mark"); //连接三个节点,形成双向链表 //jack指向tom,tom指向mark, jack.next = tom; tom.next = mark; mark.prev = tom; tom.prev = jack; //first指向jack,做为双向链表的头节点; //last直系那个mark,做为双向链表的尾节点; Node first = jack; Node last = mark; //从头到尾进行遍历 while (true) { if (first == null){ break; } //输出信息 System.out.println(first); first= first.next; } } } //定义一个node类,node对象表示双向链表的一个节点 class Node { public Object item;//存放非数据 public Node next; public Node prev; //创建一个构造器 public Node(Object item) { this.item = item; } @Override public String toString() { return "Node name=" + item; } }
原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/273403.html