
Before inserting or deleting
an item in a linked list rep-
resenting a queue,always
check if the linked list is
empty.Not doing so results
in a NullPointerException at
run time.
COMMON ERROR
TRAP
14.5 Array Representation of Stacks 1043
If the list is not empty, control skips to line 30, where we attach pn at the end
of the list by setting the next instance variable of tail to pn. We then assign pn
to tail in order to reflect that pn is now the last node of the list. Finally, and in
all cases (empty list or not), we increment numberOfItems by 1 at line 33.
Figures 14.10a to 14.10d show the impact on the list of executing lines 22,
30, and 31 step by step ...