 MODULE ListModule

   ! ====================== Specification part ===============

   TYPE ListElem
     REAL                    :: value;
     TYPE(ListElem), POINTER :: next;    
   END TYPE ListElem

 CONTAINS
   ! ====================== Implementation part ===============

   ! -----------------------
   ! Insert at head
   ! -----------------------
   FUNCTION InsertList(head, elem)
    IMPLICIT NONE

    type( ListElem ), pointer :: head, elem 
    type( ListElem ), pointer :: InsertList

    elem%next => head 
    InsertList => elem
   END FUNCTION

   ! -----------------------
   ! Delete at head
   ! -----------------------
   FUNCTION DeleteList(head)
    IMPLICIT NONE

    type( ListElem ), pointer :: head
    type( ListElem ), pointer :: DeleteList

    type( ListElem ), pointer :: h


    IF ( ASSOCIATED(head) ) THEN
       h => head
       head => head%next
       deallocate(h)
    END IF

    DeleteList => head
   END FUNCTION

   ! -----------------------
   ! Print
   ! -----------------------
  SUBROUTINE PrintList(head)
    IMPLICIT NONE

    type( ListElem ), pointer :: head
    type( ListElem ), pointer :: ptr

    ptr => head

    print *, "The list is: "
    DO WHILE ( associated(ptr) )
      print *, ptr%value
      ptr => ptr%next
    END DO
    print *
   END SUBROUTINE

END MODULE
