有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用于双链接列表的java添加节点方法?

我现在有这个,但我想把我的列表变成一个双链接列表,但不知道怎么做

public void addDNode(DNode v)
{
    if(header == tail)
    {

        header = v;
    }
    else
    {
        DNode current = header;
        while (current.nextNode() != null)
        {
            current = current.nextNode();
        }
        current.setNext(v); 
    }
}

共 (2) 个答案

  1. # 1 楼答案

    public void addDNode(DNode v) {
        if (header == null) {  // means list is empty, so add first element
            if (tail != null)  
                throw new AssertionError(); // if head points to null then tail should too
    
            header = v;
            tail = header;  // first element so (head == tail)
        } else {
            tail.setNext(v);
            v.setPrev(tail);
            v.setNext(null);
            tail = v;
        }
    }