Add node method for a double linked list? [closed]
-
11-03-2021 - |
質問
I currently have this but I want to make my list into a double linked list and can't work out how to do.
public void addDNode(DNode v)
{
if(header == tail)
{
header = v;
}
else
{
DNode current = header;
while (current.nextNode() != null)
{
current = current.nextNode();
}
current.setNext(v);
}
}
解決
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;
}
}
他のヒント
Here is what a doubly linked list is: doubly linked list article This is a one way to implement it in Java: doubly linked list example
Search on the internet, try implementing it and then if you have some issues ask here ;)
所属していません StackOverflow