/* PL/I program to create a one-way linked list. */
/* Copyright (c) 1996 by R. A. Vowels, from "Introduction to PL/I, Algorithms, and */
/* Structured Programming". All rights reserved. */
/* This procedure creates a one-way linked list, and then traverses that list. */
/* The procedure reads in integer values in free format, and stores each value in a node. */
/* As each value is read in, a node is created, the value is placed in the node, and the */
/* node is attached to the head of the list. The last of the values read in is zero (which */
/* causes the list-creation loop to terminate). Finally, the list is traversed. */
CREATE:
PROCEDURE OPTIONS (MAIN);
DECLARE 1 Node BASED (Head), /* A template for the node & the pointer to it. */
2 Value FIXED BINARY, /* For the value. */
2 Ptr POINTER; /* For a pointer to the next node. */
DECLARE Current POINTER; /* An auxiliary pointer. */
DECLARE Head POINTER;
DECLARE NULL BUILTIN;
/* The list is created by placing each new item at the head of the list. */
PUT LIST ( 'This program creates a linked list by attaching to the head of the list.' );
PUT SKIP LIST ( 'Please type the values of each node; the last value is zero:' );
/* SECTION TO CREATE A LINKED LIST. */
/* ORGANIZE THE HEAD. */
ALLOCATE Node; /* Create the node. */
GET LIST (Head -> Value); /* Place a value in the node. */
Current = Head; /* Point at the node. */
Current -> Ptr = NULL; /* A null link for the node (which is also the */
/* tail). */
/* APPEND NODES. */
/* A loop to create other nodes and to attach them to the head of the list. */
DO WHILE (Head -> Value ^= 0); /* Until a sentinel of zero is read in. */
ALLOCATE Node; /* Create a new node, to become the head. */
GET LIST (Head -> Value); /* Read in a value & place it in the new node. */
Head -> Ptr = Current; /* Make the new node point at the head of the */
/* existing list. */
Current = Head; /* Point at the new node, which has become the */
/* head of the list. */
END;
/* SECTION TO TRAVERSE the linked list, printing each element as we go. */
PUT SKIP LIST ( 'The elements in the list are:' );
Current = Head; /* Start at the Head node. */
DO WHILE (Current ^= NULL); /* Until we reach the tail. */
PUT LIST (Current -> Value);
Current = Current -> Ptr; /* Point at the next node. */
END;
END CREATE;