其他分享
首页 > 其他分享> > LinkedList双向链表

LinkedList双向链表

作者:互联网

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;
    }
}

  

 

标签:Node,LinkedList,双向,mark,链表,jack,tom,public
来源: https://www.cnblogs.com/nzm-2019/p/16463167.html