!------------------------------------------------------------------------------
!
!  Module name:
!
!    polyline
!
!  Purpose:
!
!    This module defines point and vertex data types and contains two
!    module procedures:
!
!      delentry - delete the vertex which contains the given y 
!                 coordinate  
!
!      print_list - transverse the polyline to print x and y coordinates
!                 of all vertices
!
!------------------------------------------------------------------------------

MODULE polyline

  IMPLICIT NONE

  TYPE point                            ! point type definition
    REAL x,y
  END TYPE POINT

  TYPE vertex                           ! vertex type definition
    TYPE(point) pt
    TYPE(vertex), POINTER :: link
  END TYPE vertex

CONTAINS

  ! Module procedures

  ! delentry - delete the vertex which contains the given y coordinate  

  SUBROUTINE delentry(yval, head)

    REAL yval
    TYPE(vertex), POINTER :: head
    TYPE(vertex), POINTER :: start, previous

    start => head
    DO
      IF (.NOT. ASSOCIATED(start)) EXIT         ! meet tail of list
      IF (ABS(start%pt%y - yval) < 0.01) EXIT   ! match the given y value
      previous => start
      start => start%link
    END DO

    IF (ASSOCIATED(start)) THEN                 ! y value found
      IF (ASSOCIATED(head, start)) THEN         ! it is in head of list
        head => start%link
      ELSE                                      ! not head of list
        previous%link => start%link
      ENDIF
      DEALLOCATE(start)
    ENDIF

  END SUBROUTINE delentry

  ! print_list - transverse the polyline to print x and y coordinates
  !              of all vertices

  SUBROUTINE print_list(head)

    TYPE(vertex), POINTER :: head
    TYPE(vertex), POINTER :: current

    current => head
    DO
      IF (.NOT. ASSOCIATED(current)) EXIT       ! tail of list
      WRITE (*,*) current%pt
      current => current%link
    END DO

  END SUBROUTINE print_list

END MODULE polyline






