module link_module type link real :: n type (link), pointer :: next end type link end module link_module program ch2202a use link_module implicit none type (link), pointer :: root, current integer :: i = 0, n integer :: io_stat_number = 0 real, allocatable, dimension (:) :: x character(len=40):: fname print *,'file containing real numbers?' read '(A)',fname open(unit=1,file=fname,status='old') allocate (root) ! read in 1st number from file read (unit=1,fmt=*,iostat=io_stat_number) root%n if (io_stat_number /= 0) then ! assume end of data (i.e. eof) nullify (root%next) else i = i + 1 allocate (root%next) end if current => root ! read in remaining numbers (1 per line) do while (associated(current%next)) current => current%next read (unit=1,fmt=*,iostat=io_stat_number) current%n if (io_stat_number/=0) then nullify (current%next) else i = i + 1 allocate (current%next) end if end do n = i allocate (x(1:n)) i = 1 current => root do while (associated(current%next)) x(i) = current%n i = i + 1 current => current%next end do print *,n,' numbers read from file' print *,n,' elements of array x are:' do i=1,n print*,x(i) end do end program ch2202a