How can I implement a linked list in fortran 2003-2008

≯℡__Kan透↙ 提交于 2019-11-27 18:35:58

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/8648779/how-can-i-implement-a-linked-list-in-fortran-2003-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!