traverse linked list (Fortran 90)
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

 Up
traverse linked list (Fortran 90)         

Group: comp.lang.fortran · Group Profile
Author: e p chandler
Date: Sep 16, 2008 11:30

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

--- If the list is NOT empty, add the newly created node onto the
chain.

-- e
7 Comments
diggit! del.icio.us! reddit!