PROGRAM Linked_List
!---------------------------------------------------------
! Program to construct a linked list of integers and then
! traverse the list.  Identifiers used are:
!   FirstPtr : pointer to first node in the list
!   Number   : integer read and inserted into the list
!   TempPtr  : pointer used in constructing the list
!   CurrPtr  : pointer used in traversing the list
!   Data     : node component that stores an integer
!   Next     : node component that points to the next node
!   Response : user response to more-data query
!
! Input:   Integers
! Output:  Integers stored in the list
!---------------------------------------------------------

   TYPE List_Node
      INTEGER :: Data
      TYPE (List_Node), POINTER :: Next
   END TYPE List_Node

   TYPE (List_Node), POINTER :: FirstPtr, TempPtr, CurrPtr

   INTEGER :: Number
   CHARACTER (LEN = 1) Response

   ! Initialize an empty list
   NULLIFY (FirstPtr)

   ! Read the integers and construct the list
   DO
      PRINT *, "Enter number to add to list:"
      READ *, Number
      ALLOCATE (TempPtr)
      TempPtr%Data = Number
      TempPtr%Next => FirstPtr
      FirstPtr => TempPtr

      PRINT *, "More data (Y or N)? "
      READ *, Response
      IF (Response == "N") EXIT
   END DO

   ! Traverse the list and display the integers (in reverse order)
   PRINT *
   PRINT *, "Numbers in the list are:"
   CurrPtr => FirstPtr
   DO
      IF (.NOT. ASSOCIATED(CurrPtr)) EXIT
      PRINT *, CurrPtr%Data
      CurrPtr => CurrPtr%Next
   END DO

END PROGRAM Linked_List