Code
fortran
module linked_list
implicit none
type :: node
integer :: value
type(node), pointer :: next => null()
end type
type :: list_type
type(node), pointer :: head => null()
integer :: count = 0
contains
procedure :: push
procedure :: print => print_list
final :: free_list
end type
contains
subroutine push(self, val)
class(list_type), intent(inout) :: self
integer, intent(in) :: val
type(node), pointer :: new_node
allocate(new_node)
new_node%value = val
new_node%next => self%head
self%head => new_node
self%count = self%count + 1
end subroutine
subroutine print_list(self)
class(list_type), intent(in) :: self
type(node), pointer :: cur
cur => self%head
do while (associated(cur))
print *, cur%value
cur => cur%next
end do
end subroutine
subroutine free_list(self)
type(list_type), intent(inout) :: self
type(node), pointer :: cur, tmp
cur => self%head
do while (associated(cur))
tmp => cur%next
deallocate(cur)
cur => tmp
end do
end subroutine
end module