How to implement a linked list in fortran 2003-2008

I need to implement a link list data structure for my molecular dynamics code in fortran 2003/2008 I use the latest fortran (Intel) compilers.

How can I implement a linked list in the best way, I would prefer without blocking without waiting, if possible in Fortran.

Thanks.

+4
source share
1 answer

This is easiest if you create a user-defined type with your data elements and a pointer to the next element. This assumes a singly linked list. eg.

type MyList_type integer :: FirstItem real :: SecondItem etc type (MyList_type), pointer :: next_ptr => null () end type MyList_type 

Then create the first item with "allocate". After that, you write code to move through the list, using next_ptr to move through the list. Use the "linked" internal function to check if next_ptr is defined yet, or if you have reached the end of the list.

If you are writing a regular Fortran sequential program, then locking / no waiting is not a problem. If you are writing a multi-threaded / parallel program, then consistent access to variables is a problem.

Here are some more examples: http://fortranwiki.org/fortran/show/Linked+list . Moreover, Fortran related lists are clearly explained in the book Fortran 90/95 Explained by Metcalf and Reed.

+6
source

Source: https://habr.com/ru/post/1388193/


All Articles