M_generic_list(3f) - [M_generic_list::INTRO] A Generic Linked List Implementation in Fortran 95 (LICENSE:MIT)
Synopsis
Description
Author
Examples
Usage:
public :: list_node_t, list_data public :: list_init, list_free public :: list_insert, list_put, list_get, list_next
A linked list, or more specifically a singly-linked list, is a list consisting of a series of individual node elements where each node contains a data member and a pointer that points to the next node in the list. M_generic_list(3fm) defines a generic linked list implementation in standard Fortran 95 which is able to store arbitrary data (and in particular -- pointers to arbitrary data types).
M_generic_list(3fm) defines a Generic Linked List Implementation in Fortran 95. This module, described in detail at http://fortranwiki.org is by:
Jason R. Blevins Department of Economics, Duke University May18,2009
Sample program:
! program demo_M_generic_list and module module data1 implicit noneprivate public :: data_t public :: data_ptr
! Data is stored in data_t type :: data_t real :: x end type data_t
! A container for storing data_t pointers type :: data_ptr type(data_t), pointer :: p end type data_ptr
end module data1
program demo_M_generic_list use M_generic_list use data1 implicit none
type(list_node_t), pointer :: list => null() type(data_ptr) :: ptr
! Allocate a new data element allocate(ptr%p) ptr%p%x = 2.7183
! Initialize the list with the first data element call list_init(list, transfer(ptr, list_data)) print *, Initializing list with data:, ptr%p
! Allocate a second data element allocate(ptr%p) ptr%p%x = 0.5772
! Insert the second into the list call list_insert(list, transfer(ptr, list_data)) print *, Inserting node with data:, ptr%p
! Retrieve data from the second node and free memory ptr = transfer(list_get(list_next(list)), ptr) print *, Second node data:, ptr%p deallocate(ptr%p)
! Retrieve data from the head node and free memory ptr = transfer(list_get(list), ptr) print *, Head node data:, ptr%p deallocate(ptr%p)
! Free the list call list_free(list) end program demo_M_generic_list
Nemo Release 3.1 | M_generic_list (3) | February 23, 2025 |