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

折月煮酒 提交于 2019-11-29 04:43:28

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.

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