Example: using pointers to process a chained list
The following example shows how you might process a linked list, that is, a chained list of data items.
For this example, picture a chained list of data that consists of individual salary records. The following figure shows one way to visualize how the records are linked in storage. The first item in each record except the last points to the next record. The first item in the last record contains a null value (instead of a valid address) to indicate that it is the last record.
The high-level pseudocode for an application that processes these records might be:
Obtain address of first record in chained list from routine
Check for end of the list
Do until end of the list
Process record
Traverse to the next record
End
The following code contains an outline of the calling
program, LISTS
, used in this example of processing
a chained list.
IDENTIFICATION DIVISION.
PROGRAM-ID. LISTS.
ENVIRONMENT DIVISION.
DATA DIVISION.
******
WORKING-STORAGE SECTION.
77 PTR-FIRST POINTER VALUE IS NULL. (1)
77 DEPT-TOTAL PIC 9(4) VALUE IS 0.
******
LINKAGE SECTION.
01 SALARY-REC.
02 PTR-NEXT-REC POINTER. (2)
02 NAME PIC X(20).
02 DEPT PIC 9(4).
02 SALARY PIC 9(6).
01 DEPT-X PIC 9(4).
******
PROCEDURE DIVISION USING DEPT-X.
******
* FOR EVERYONE IN THE DEPARTMENT RECEIVED AS DEPT-X,
* GO THROUGH ALL THE RECORDS IN THE CHAINED LIST BASED ON THE
* ADDRESS OBTAINED FROM THE PROGRAM CHAIN-ANCH
* AND ACCUMULATE THE SALARIES.
* IN EACH RECORD, PTR-NEXT-REC IS A POINTER TO THE NEXT RECORD
* IN THE LIST; IN THE LAST RECORD, PTR-NEXT-REC IS NULL.
* DISPLAY THE TOTAL.
******
CALL "CHAIN-ANCH" USING PTR-FIRST (3)
SET ADDRESS OF SALARY-REC TO PTR-FIRST (4)
******
PERFORM WITH TEST BEFORE UNTIL ADDRESS OF SALARY-REC = NULL (5)
IF DEPT = DEPT-X
THEN ADD SALARY TO DEPT-TOTAL
ELSE CONTINUE
END-IF
SET ADDRESS OF SALARY-REC TO PTR-NEXT-REC (6)
END-PERFORM
******
DISPLAY DEPT-TOTAL
GOBACK.
- (1)
PTR-FIRST
is defined as a pointer data item with an initial value ofNULL
. On a successful return from the call toCHAIN-ANCH
,PTR-FIRST
contains the address of the first record in the chained list. If something goes wrong with the call, andPTR-FIRST
never receives the value of the address of the first record in the chain, a null value remains inPTR-FIRST
and, according to the logic of the program, the records will not be processed.- (2)
- The
LINKAGE SECTION
of the calling program contains the description of the records in the chained list. It also contains the description of the department code that is passed in theUSING
clause of theCALL
statement. - (3)
- To obtain the address of the first
SALARY-REC
record area, theLISTS
program calls the programCHAIN-ANCH
. - (4)
- The
SET
statement bases the record descriptionSALARY-REC
on the address contained inPTR-FIRST
. - (5)
- The chained list in this example is set up so that the last record
contains an address that is not valid. This check for the end of the
chained list is accomplished with a do-while structure where the value
NULL
is assigned to the pointer data item in the last record. - (6)
- The address of the record in the
LINKAGE-SECTION
is set equal to the address of the next record by means of the pointer data item sent as the first field inSALARY-REC
. The record-processing routine repeats, processing the next record in the chained list.
To increment addresses received from another program,
you could set up the LINKAGE SECTION
and PROCEDURE
DIVISION
like this:
LINKAGE SECTION.
01 RECORD-A.
02 HEADER PIC X(12).
02 REAL-SALARY-REC PIC X(30).
. . .
01 SALARY-REC.
02 PTR-NEXT-REC POINTER.
02 NAME PIC X(20).
02 DEPT PIC 9(4).
02 SALARY PIC 9(6).
. . .
PROCEDURE DIVISION USING DEPT-X.
. . .
SET ADDRESS OF SALARY-REC TO ADDRESS OF REAL-SALARY-REC
The address of SALARY-REC
is now
based on the address of REAL-SALARY-REC
, or RECORD-A
+
12.