Finding the Previous Node

(The following discussion uses data names defined elsewhere.)

The following Cobol code searches a linked list to find the node just prior to the node to which CURR-P points:

     SET PREV-P                        TO FIRST-P.
*
     IF PREV-P NOT = NULL
        SET ADDRESS OF TN-NODE         TO PREV-P
        PERFORM UNTIL PREV-P = NULL
        OR TN-NEXT-P = CURR-P 
           SET PREV-P                  TO TN-NEXT-P
           IF PREV-P NOT = NULL
              SET ADDRESS OF TN-NODE   TO PREV-P
           END-IF 
        END-PERFORM.
This procedure is almost identical to that described elsewhere for searching the list. The only significant differences are: The above code fragment finishes with TN-NODE representing the node prior to the current node. In the context of the entire program, we might want to add the line:
     SET ADDRESS OF TN-NODE TO CURR-P.
...because the rest of the program might expect CURR-P to be pointing to TN-NODE.
[home]Cobol Home