Thursday, July 30, 2009

C++ question, test review?

The pointer “head” points to the start of a linked list, “p” points to a node of that linked list, “q” points to the node following the node that “p” points to. What is the effect of the following statements?


newNode = new nodeType;


newNode-%26gt;info = 42;


newNode-%26gt;link = q;


head = newNode;


newNode-%26gt;link = head;





a. A node will be inserted at the start of the list.


b. A node will be inserted between p and q.


c. A node will be inserted after q.


d. None of the above, there is an error








Thanks in advance guys

C++ question, test review?
d. 'head' is pointing to the new node, which makes it the defacto start of the list, but the new nodes link should be pointing to the old first node for that to work properly. The above code first points to the 'q' node which is in the list but not ncessarily the start of it, cutting off all the nodes between the old head and q. Then the link is set to point to itself, cutting off the rest of the list.
Reply:a.


No comments:

Post a Comment