CS 1704 Fall 2002 Homework 4 Answers Q A Reason ------------------------------------------------------------------------------------ 1 2 Straight from the declaration. 2 5 *Head is the target of Head, which would be an SNode. 3 2 Straight from the declaration of Next in SNode. 4 3 Straight from the declaration of Element in SNode. 5 6 ptr hasn't been declared, so the expression doesn't make any sense. Actually, "ptr" should have been "Head". But even in that case, the expression wouldn't make any sense either, since Head->Next->Element is an integer, not a pointer, and therefore Head->Next->Element->Next doesn't make sense since it attempts to dereference an integer. 6 6 Head isn't a struct or a class, so you can't apply the field selector '.' to it. 7 4 &Head would be the address of an SNode*, which would be an SNode**. Head->Next is an SNode*. (*Head).Next is equivalent to Head->Next. Since Head->Next is an SNode*, dereferencing it yields an SNode. 8 8 Head++ doesn't make sense unless Head points to an array. Head->Next points to the second node, so this would make Head point to the second node. This is equivalent to Head = Head->Next. This attempts to assign an SNode to an SNode*; it won't compile. This makes the first node point to itself. The given code results in the structure: +--+--+ +--+------+ q--->| 5| -|-->|12| NULL |<---p +--+--+ +--+------+ 9 2 q points to an SNode, not an integer. The element field of the target of q stores the value 5. q->Next would be the target of p, so this would be 12. q->Next points to an SNode, not an integer. p points ot an SNode, not an integer. The element field of the target of p stores the value 12. p->Next is NULL. p->Next->Element is undefined since p->Next is NULL. 10 6 See the discussion above. 11 4 q->Next points to the target of p; the pointer field of that is NULL. 12 3 You need a statement to make the node containing 18 point to the node containing 32. 13 4 You need to make the first node point to the third node. Head->Next is the pointer field of the first node; Head->Next->Next points to the third node. 14 6 Head->Element is the data field of the first node. Head->Next points to the second node, so Head->Next->Element will do it. This is equivalent to the previous choice. headPtr hasn't been declared; if it were Head, then *(Head->Next) would be an SNode, not an int. 15 9 The first three work. The fourth code fragment attempts to advance the Current pointer with an illegal statement. The fifth code fragment also attempts to advance the Current pointer with an illegal statement. 16 3 The first two include a test for the Current pointer being initially NULL. The third one does not, and an access violation will occur in the loop test if the list is empty. 17 7 2 fails because the loop test is incorrect; the last node will never be processed. 4 fails because the output statement looks ahead one level, and the first node will not be processed. 18 3 1 doesn't reset Previous->Next "around" the next node, and then it deletes *Previous. 2 moves previous instead of reseting Previous->Next "around" the next node. 4 doesn't reset Previous->Next "around" the next node. For 19 through 24, you just need to trace the effect of the given code on the given list: 19 7 20 4 21 2 22 6 23 3 24 10 25 3 When delete is invoked on a pointer to an object, the destructor for that object is executed. In this case, delete p results in calling delete on p->Next, which deallocates the last node in the list. So, the last two nodes are deallocated. Worse, the pointer in the second node isn't reset, so it still targets the deallocated third node.