Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I 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 am using the newest fortran compilers (Intel).

How do I come about implement the linked list in the best way possible I would prefer a lock-free no wait implementation if possible in Fortran.

Thank you.

like image 417
David MZ Avatar asked Dec 27 '11 20:12

David MZ


1 Answers

It is easiest if you create a user defined type with your data items and the pointer to the next item. This is assuming a singly-linked list. e.g.,

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

Then create the first member with "allocate". Thereafter you write code to traverse the list, using next_ptr to step through the list. Use the "associated" intrinsic function to test whether next_ptr is defined yet, or instead you have reached the end of the list.

If you are writing an ordinary sequential Fortran program then lock-free/no-wait is not an issue. If you are writing a multi-threaded / parallel program, then consistent access to the variables is an issue.

Here are some more examples: http://fortranwiki.org/fortran/show/Linked+list. Even better, linked lists in Fortran are clearly explained in the book "Fortran 90/95 Explained" by Metcalf and Reid.

like image 193
M. S. B. Avatar answered Oct 22 '22 06:10

M. S. B.