If there were 2 nodes like this:
http://i.imgur.com/21x2m.png

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

I smell a homework problem....

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?

Assuming:

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.

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:
http://i.imgur.com/mT8HT.png

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.