!-----------------------------------------------------------------------
!
!  Program name:
!
!    run_polyline
!
!  Purpose:
!
!    This program uses the module polyline to build and transverse a 
!    polyline linked list containing connected vertices.
!
!    Notice that the linked list preserves the order the read points.
!
!-----------------------------------------------------------------------

PROGRAM run_polyline1

  USE polyline   	! makes all module definitions available

  IMPLICIT NONE

  REAL :: yval		! the y coordinate of a point
  TYPE(point) :: pt
  TYPE(vertex), POINTER :: current, first, last
  INTEGER :: status

  ! open files to read points and y value   

  OPEN (1, FILE = 'polyline.dat')

  ! build up a polyline list
   
  NULLIFY(first, last)		! initially nullify list (empty)

  READ(1, *) pt			! read point from channel 1
  IF (ABS(pt%x - 999.999) > 0.001) THEN		! if 999.999 do nothing
    ALLOCATE(first, STAT = status)		! create first node of list
    IF (status > 0) STOP 'Fail to allocate a new vertex'
    first%pt = pt     		! give the point
    NULLIFY(first%link) 	! point to null (end of list)
    last => first		! update last of list

    DO
      READ(1, *) pt		! read point from channel 1 until 999.999 
      IF (ABS(pt%x - 999.999) < 0.001) EXIT
      ALLOCATE(current, STAT = status)		! create new node
      IF (status > 0) STOP 'Fail to allocate a new vertex'
      current%pt = pt     	! give the point
      NULLIFY(current%link)	! point to null (end of list)
      last%link => current      ! link to last of list
      last => current		! update last of polyline list
    END DO  
  END IF
  
  WRITE (*, *) 'Printing the list for the first time'
  CALL print_list(first)

  WRITE(*, *)
  WRITE(*, *) 'Input the y value of the point to be deleted from the list' 
  READ (*, *) yval
  WRITE(*, *)
  WRITE(*, *) 'Looking up the list and deleting the node with the y value'
  CALL delentry(yval, first)

  WRITE(*, *) 'Here is the new list:'
  CALL print_list(first)

END PROGRAM run_polyline1
  


