Jump to content
  • Advertisement
Sign in to follow this  
hitesh015

doubly linked list in c

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

If you intended to correct an error in the post then please contact us.

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??? Doesn`t it make this list a circular one, while it should be doubly linked list.

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!