! Program Example from "Essential Fortran 90 & 95" by Loren P. Meissner
! Copyright 1996. Copying for sale requires permission from the author.
! Otherwise, distribution is permitted if these three lines are included.

  module I06M
    implicit none
    public :: Look_Up
    integer, parameter, public :: S_LEN = 20
    type, public :: Box_Type
      character (len = S_LEN) :: Name
      type (Box_Type), pointer :: Next_BP
    end type Box_Type
  contains
    recursive subroutine Look_Up( Arg_BP, A_Name )
      type (Box_Type), pointer :: Arg_BP
      character (len = *), intent (in) :: A_Name
      type (Box_Type), pointer :: Temp_BP
  ! start subroutine Look_Up
      if (associated( Arg_BP )) then
        if (A_Name == Arg_BP % Name) then
          write (unit = *, fmt = *) A_Name, " Found. "
          return
        else if (A_Name < Arg_BP % Name) then   ! Search has passed insertion point.
        else  ! Keep looking.
          call Look_Up( Arg_BP % Next_BP, A_Name )   ! Recursion
          return
        end if
      end if
      write (unit = *, fmt = *) A_Name, " Not found. "   ! Insert a new item.
      allocate( Temp_BP )
      Temp_BP % Name = A_Name   ! Copy pointers to link new item.
      Temp_BP % Next_BP => Arg_BP
      Arg_BP => Temp_BP
      return
    end subroutine Look_Up
  end module I06M
