PROGRAM Patients
IMPLICIT NONE
TYPE Link
   CHARACTER(LEN=6)::Patient_number=' '
   CHARACTER(LEN=20)::ward=' '
   TYPE(Link), POINTER:: Next=>NULL()
END TYPE Link
CHARACTER::More='N'
TYPE(Link), POINTER::Root, Current
   !
   ! allocate space for 1st item in list
   !
   ALLOCATE(Root)
   PRINT*,'input Patient number:'
   READ '(A)',Root%Patient_number
   PRINT*,'input hospital ward for this patient:'
   READ '(A)',Root%ward
   !
   ! allocate space for 2nd item in list (will be a blank item at end)
   !
   ALLOCATE(Root%next)
   PRINT*,'another patient? (Y/N)'
   READ'(A)', More
   Current=>Root
   !
   ! current points to beginning of list
   !
   DO WHILE(More=='Y'.OR.More=='y')
      !
      ! current points to next item in list
      !
      Current=>Current%next
      PRINT*,'input Patient number:'
      READ '(A)',Current%Patient_number
      PRINT*,'input hospital ward for this patient:'
      READ '(A)',Current%ward
      !
      ! allocate space for next item in list (will be a blank item at end)
      !
      ALLOCATE(Current%next)
      PRINT*,'another patient? (Y/N)'
      READ'(A)', More
   END DO
   !
   ! Print out patient numbers and wards
   !
   PRINT 100
   100 FORMAT(1X,'Patient number',3X,'ward')
   Current=>Root
   DO 
      PRINT 200,Current%Patient_number,Current%ward
      200 FORMAT(5X,A,7X,A)
      !
      ! current points to next item in list
      !
      Current=>Current%next
   IF(.NOT. Associated(Current%next)) EXIT
   END DO
END PROGRAM Patients

