Public Group

This topic is 3339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

i have a code segment of doubly linked list implementation: adding a node in the middle of list pNew -> left = pCur; pNew -> right = pCur -> right; pCur -> right -> left = pNew; pCur -> right = pNew; what is the purpose of the last line of abive code??? Doesnt it make this list a circular one, while it should be doubly linked list.

Share on other sites
Without that pCur->right would refer to the next node after pNew, and coming from the left you wouldn't know pNew is there.

Share on other sites
Moved to the For Beginners forum. You should post these sorts of questions there.

Share on other sites
Lets draw it out:
Start: (left pointers come out near the top of a node and right pointers come out near the bottom)             +------+             |      |             | pNew |             |      |             +------++------+                  +------+|      | ---------------> |      || pCur |                  |  X   ||      | <--------------- |      |+------+                  +------+pNew -> left = pCur;             +------+    +------- |      |    |        | pNew |    |        |      |    |        +------+    V+------+                  +------+|      | ---------------> |      || pCur |                  |  X   ||      | <--------------- |      |+------+                  +------+pNew -> right = pCur -> right;             +------+    +------- |      |    |        | pNew |    |        |      | --------+    |        +------+         |    V                         V+------+                  +------+|      | ---------------> |      || pCur |                  |  X   ||      | <--------------- |      |+------+                  +------+pCur -> right -> left = pNew;//pCur->right points to the node I've marked as X,//so the above is the same as X->left = pNew             +------+    +------- |      |    |        | pNew |    |        |      | --------+    |        +------+         |    V                         V                 ^+------+         |        +------+|      | --------+------> |      || pCur |         |        |  X   ||      |         +------- |      |+------+                  +------+pCur -> right = pNew;             +------+    +------- |      |    |        | pNew |    |        |      | --------+    |        +------+         |    V                         V               ^  ^+------+       |  |       +------+|      | ------+  |       |      || pCur |          |       |  X   ||      |          +------ |      |+------+                  +------+`

Looks correct to me! Did you follow it?

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• Forum Statistics

• Total Topics
633710
• Total Posts
3013486
×