See
http://www.pcc.qub.ac.uk/tec/courses/f77tof90/stu-notes/f90studentMIF_6.html
for code which creates, traverses and destroys a linked list. Note
that this is a stack - last in - first out.
I found the code a bit quirky and decided to write my own so that the
list would be traversed in the order in which items were entered. So I
sat down and wrote out the following on a legal pad. It compiled and
ran on the first try!
program linked_list
implicit none
type node
integer :: i
type(node), pointer :: p
end type node
type(node), pointer :: h, t, c
integer :: i
nullify(h)
do
read *, i
if ( i == 0 ) exit
allocate(c)
c%%i = i
nullify(c%%p)
if ( .not. associated(h) ) then
h => c
else
t%%p => c
end if
t => c
end do
do
c => h
if ( .not. associated(c) ) then
exit
else
print *, c%%i
h => c%%p
deallocate(c)
end if
end do
end program linked_list
I acutally understand what this code does! The only subtle part is
t%%p => c