!LIST_DATA will be renamed to other type !the actual linked list type LIST_NODE type(LIST_NODE), pointer :: next type(LIST_DATA) :: data end type type LIST type(LIST_NODE), pointer :: head end type contains !list constructor subroutine list_new(lp) type (LIST), pointer :: lp allocate (lp) !init the head to null lp%head => null() end subroutine !teardown the linked list subroutine list_destroy(lp) type(LIST), pointer :: lp type(LIST_NODE), pointer :: current, next current => lp%head do while ( associated(current) ) next => current%next deallocate (current) current => next enddo deallocate(lp) end subroutine !size function list_size(lp) integer :: list_size type(LIST), pointer :: lp !node pointer for traversing the linked list type(LIST_NODE), pointer :: np if(.not.associated(lp)) then print *, "ERROR: list_size(), list pointer is not associated." stop 1 else !start at the head of the list np=>lp%head list_size = 0 !count through the nodes do while (associated(np)) list_size = list_size + 1 np => np%next end do end if end function !add subroutine list_add(lp, data) type(LIST), pointer :: lp type(LIST_DATA) :: data type(LIST_NODE), pointer :: tail, node !error checking if(.not.associated(lp)) then print *, "ERROR: list_add(), list pointer is not associated." stop 1 end if !create the new node allocate(node) node%data = data node%next => null() if(.not.associated(lp%head)) then !first element lp%head => node else !traverse until the tail is reached tail => lp%head do while (associated(tail%next)) tail => tail%next end do tail% next=> node end if end subroutine !get !1 based index function list_get(lp, index) type(LIST_DATA), pointer :: list_get type(LIST), intent(in), pointer :: lp integer, intent(in) :: index integer :: i type(LIST_NODE), pointer :: node !error checking if(.not.associated(lp)) then print *, "ERROR: list_add(), list pointer is not associated." stop 1 end if node => lp%head if (.not.associated(node)) then print *, "ERROR: list_get(), index out of bounds", index stop 1 end if do i=1, index-1 node => node%next if (.not.associated(node)) then print *, "ERROR: list_get(), index out of bounds", index stop 1 end if end do list_get => node%data end function