2011-12-08 33 views
-3

我目前有这个,但我想把我的列表变成一个双链表,并且无法解决如何操作。为双链表添加节点方法?

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

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

java.util.LinkedList是双链表。难道你不想使用它,或只是看看里面的add()方法? – korifey

+0

解决所有链表问题的方法是绘制图表。这将帮助您精确地建立需要更新的指针/引用数量。 –

+2

@ user979236:那你是怎么写这段代码的? –

回答

2
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; 
    } 
} 
+0

谢谢但最后变量是什么? – Elliot678

+0

我修复了这个错字。 – msi

+1

它只是给人们提供完整的代码片段来解决他们的作业问题而皱起眉头...... –