The following COBOL code inserts a new node after the node to which CURR-P points (we should have already verified that CURR-P is not NULL):
PERFORM 9000-ALLOCATE-TN.
SET ADDRESS OF TN-NODE TO CURR-P.
SET ALT-TN-NEXT-P TO TN-NEXT-P.
*
IF TN-NEXT-P = NULL
SET LAST-P TO NEW-P.
*
SET TN-NEXT-P TO NEW-P.
SET NEW-P TO NULL.
Remember, 9000-ALLOCATE-TN allocates a new node and initializes it
in ALT-TN-NODE, leaving ALT-TN-NEXT-P set to NULL. The situation then
looks like this:
First we point the new node to its successor:
In the example illustrated, the current node is in the middle of the
list. If it is the last node, then we must point LAST-P at the new
node, which is now becoming the last node. If we're not maintaining
LAST-P then we don't need to give the last node any special treatment.
Next we point the current node to the new node, which becomes its successor:
Finally we nullify NEW-P:
This last step is not really necessary, but it's good hygiene. If you keep an extra copy of a pointer longer than you need to, it may lead you into a pitfall later.
Pointer Traps