views:

279

answers:

2

Hi, I am working on some code were I need to add a Node into a doubly linked-list, this is the code I have so far:

Node tempNext = cursor.getNext();
temp = new Node(item, null, cursor, tempNext);
tempNext.setPrev(temp);

cursor is the Node that is right before the new added Node should go.

+2  A: 

Seems like all you need to add is

cursor.setNext(temp);

Do you have a specific question?

JacobM
So:Node tempNext = cursor.getNext();temp = new Node(item, null, cursor, tempNext); tempNext.setPrev(temp); cursor.setNext(temp);Will successfully add a Node to the linked-list correct?
Jacob Nelson
It should, assuming the Node constructor you are using correctly sets the next and previous nodes for your new node. Also, @rodrigoap is absolutely right about meaningful variable names.
JacobM
+4  A: 

Writing meaningful variable names will always help you, never use names like temp*:

protected void insertNodeAfter(Node currentNode, Node newNode) {
    Node displacedNode = currentNode.getNext();
    currentNode.setNext(newNode);
    newNode.setNext(displacedNode)
    displacedNode.setPrev(newNode);
    newNode.setPrev(currentNode);
}
rodrigoap
-1 for answering a homework question with functional code.
Bill K
You are right Bill, I deserve it.
rodrigoap