Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
function insertAfter(List list, Node node, Node newNode)
     newNode.prev  := node
     newNode.next  := node.next
     if node.next == null
         list.lastNode  := newNode
     else
         node.next.prev  := newNode
     node.next  := newNode
Posted
Updated 17-Aug-14 23:18pm
v2
Comments
enhzflep 18-Aug-14 5:40am    
Your question is currently useless. Consider editing the title to something reasonable (e.g Doubly Linked List implementation) also, remember to add your question to the body of the question. As it stands, the unreasonable title is no big deal. However, the omission of (1/2 of) the question makes this impossible to answer without further input from yourself.

1 solution

OK...think about what a doubly linked list needs to have:
Node      1      2      3
Prev      0    <-1    <-2
Next      2->    3->    0

So when you try to insert a new node between 1 and 2, you have to affect 1's Next node, and 2's previous:
Node      1      x      2      3
Prev      0    <-1    <-x    <-2
Next      x->    2->    3->    0

So:
node.next.prev:=newNode

Is inserting the "x" reference in the "prev" link of the next node: the "x" in the "Prev" of "2" in my example.

Try it with pen and paper: it'll make sense!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900