The following COBOL code appends a second list (referenced by ALT-FIRST-P and ALT-LAST-P) to the first list (referenced by FIRST-P and LAST-P):
IF LAST-P = NULL
SET FIRST-P TO ALT-FIRST-P
ELSE
SET ADDRESS OF TN-NODE TO LAST-P
SET TN-NEXT-P TO ALT-FIRST-P.
*
SET ALT-FIRST-P TO NULL.
SET LAST-P TO ALT-LAST-P.
SET ALT-LAST-P TO NULL.
If the first list is empty, we can simply transfer ALT-FIRST-P to
FIRST-P and ALT-LAST-P to LAST-P. Otherwise the initial situation looks
like this:
First we attach the second list to the tail of the first list:
We no longer need ALT-FIRST-P, so we nullify it:
Now we redirect LAST-P to the last node of the second list, since that is the last node of the combined list:
We no longer need ALT-LAST-P, so we nullify it:
Now all the nodes are in the first list, and the second list is empty.
Ideally your own code will build and maintain both lists. If so, just make sure you maintain the lists correctly. Otherwise you may need to be more paranoid.
Sometimes it is useful to include error-checking code for testing and development, when your data structures are most vulnerable to bugs. For production use you can comment it out, as long as the rest of the code doesn't depend on the presence of the error-checking code. This strategy resembles the use of the assert() macro in C.
Pointer Traps