If there were 2 nodes like this:

Would newNode be pointing to itself if there was a code like:

newNode->next = intNode->next

Also what would intNode be pointing at if this code was right after the one above it:

intNode->next->prev = newNode
6 Years
Discussion Span
Last Post by mike_2000_17

Firstly, the picture doesn't show the connectiveness of the nodes, that is it doesn't show if the node are connected or not, so what are the assumptions?


1) intNode->next points to newNode
2) intNode->prev points to null
3) newNode->prev points to intNode
4) newNode->next points to null

Now if the above assumptions are valid then you can imagine "intNode->next" as just newNode.
hence this statement, newNode->next = intNode->next is just intNode = intNode->next which is a memory leak. You can do similar analysis on the other statement


>> newNode->next = intNode->next After that statement, the following would evaluate to true: newNode == newNode->next . That's as far as it goes. And it would certainly require special attention down the line to avoid problems when trying to delete the linked list (and traversing it in general).

>> intNode->next->prev = newNode After this line, the following would evaluate to true: newNode->prev == newNode , because intNode->next == newNode and thus, intNode->next->prev = newNode <===> newNode->prev = newNode .

So, at the end of both statements, both the newNode->next and newNode->prev would pointing to newNode itself.

Edited by mike_2000_17: n/a


Sorry for the unclear problem. This was a review exercise at the end of a chapter and I wasn't sure how the book got it's answer. This is the full question:

At each of the numbered comments, draw the doubly linked list.
intNode = new intNode<int> (10);
newNode = new dnode<int>(20);
newNode->next = intNode->next;
intNode->next->prev = newNode;
newNode->prev = intNode;
intNode->next = newNode; //#1

The answer was supposed to look like this:


The code that you posted should cause an access violation at line 5. That code is erroneous, either find the errata of the book you have or skip this exercise.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.