TAOCP 2.1 Exercise 6

The assignment `CARD ← NODE(TOP)` copies the entire node at the address `TOP` into the variable `CARD`.

Section 2.1: Introduction

Exercise 6. [06] In the playing card example, suppose that CARD is the name of a variable whose value is an entire node as in (6). The operation CARD ← NODE(TOP) sets the fields of CARD respectively equal to those of the top of the pile. After this operation, which of the following notations stands for the suit of the top card? (a) SUIT(CARD); (b) SUIT(LOC(CARD)); (c) SUIT(CONTENTS(CARD)); (d) SUIT(TOP)?

Verified: no
Solve time: -


The assignment CARD ← NODE(TOP) copies the entire node at the address TOP into the variable CARD. Consequently, the fields of CARD, including its suit, rank, and link, are independent of the original node after the assignment. Therefore, SUIT(CARD) refers to the suit of the top card, since CARD now contains a complete copy of that node. The other notations, SUIT(LOC(CARD)), SUIT(CONTENTS(CARD)), and SUIT(TOP), either refer to addresses or the original pile without regard to the copied fields and are not affected by the assignment.

Hence, the correct notation for the suit of the top card after the assignment is $\boxed{\text{SUIT(CARD)}}$.