!========================================================
!
!  some samples of advanced capabilities in Fortran 90
!
!  -- MODULES, DYNAMIC MEMORY ALLOCATION, DERIVED TYPES
!    + poointers
! -- + CASE and LINKED LISTS
!========================================================

!
!  MODULES are used for easy sharing of global information
!
module listdef
   implicit none

   type mylist
       type (mylist), pointer :: next   ! note recursion!
       integer :: mynumber
       integer :: somenumber
   end type mylist


end module listdef


!================= MAIN PROGRAM ==============================
!
!  this program searches for a random sequence of integers
!
   use listdef
   implicit none
!
!  INTERFACES help make sure subroutines are correctly accessed
!  particularly useful when passing back and forth pointers and/or
!  allocatable arrays
!
   interface
   end interface

   integer ns  ! # of integers in a sequence
   integer, allocatable :: nlist(:)

   integer ntot ! total # of integers so far
   integer i
   logical ok

   ntot = 0
1  continue
   print*,' Enter # of integers in sequence (0 to end) '
   read*,ns
   if( ns < 0)ns = 0
!----------------- EXAMPLE USE OF SELECT CASE ------
   select case (ns)
      case (0)
          print*,' Okay, goodbye ! '
          stop

      case (1)
          print*,' that  is too short to make a sequence '
          goto 1

      case default
          if(allocated(nlist))deallocate(nlist)
          allocate(nlist(ns) )
          print*,' Enter target digits '
          do i = 1,ns
             ok = .false. 
             do while(.not. ok)
                read*,nlist(i)
                if(nlist(i) < 0 .or. nlist(i) > 9)then
                    print*,' digits 0 through 9 only, please '
                else
                    ok = .true.
                endif
             end do
          end do 
   end select

!--------- GENERATE RANDOM NUMBERS

   
   

   end   ! main program

!------------------------------------------------------------

