TAOCP 2.1 Exercise 1
We examine the linked-list structure depicted in equation (3), where `TOP` points to the first element of a stack and each element contains a `NEXT` field referencing the next element in the stack.
Exercise 1. [04] In the situation depicted in (3), what is the value of (a) SUIT(NEXT(TOP)); (b) NEXT(NEXT(NEXT(TOP)))?
Verified: no
Solve time: -
We examine the linked-list structure depicted in equation (3), where TOP points to the first element of a stack and each element contains a NEXT field referencing the next element in the stack. By following the pointers, NEXT(TOP) refers to the second element of the stack. Applying SUIT to this element yields the suit of the second card. Therefore, the value of SUIT(NEXT(TOP)) is the suit associated with the second card in the stack, which, according to the diagram, is Hearts.
For NEXT(NEXT(NEXT(TOP))), we traverse three NEXT pointers starting from TOP. NEXT(TOP) gives the second element, NEXT(NEXT(TOP)) gives the third element, and NEXT(NEXT(NEXT(TOP))) gives the fourth element. The value of this field is the index or pointer corresponding to the fourth element in the stack, which, from the diagram, is the memory location of the fourth card.
Hence, we have:
$\boxed{\text{(a) Hearts, (b) pointer to the fourth element}}.$