- #1
zmth
- 29
- 1
CAn anyone explain how this program in Fortran on linked lists is supposed to work. Actually it does work but I can not for the life of me follow the logic behind it or what is going on. I did some Fortran programming in F77 but now with this new 'pointer' type it is about impossible to understand. I have done assembly language programming so if one could translate how this is translated to assembly language of the microprocessor then i may be able to understand. Like what memory locations and what is being put in them and what is being read from the locations.
One big problem is that if i change the => to just = then it does not work ! HOw can => mean anything other than = ? And if i change the statement current%i = number to
current%i = pointer then it does NOT work. CAn anyone explain why not ? Why does
current%i = number work and current%i = pointer NOT work.
Fortran:
program linkedlist
implicit none
type :: link
integer :: i
type (link), pointer :: next
end type link
type (link), pointer :: first, current
integer :: number
nullify (first)
nullify (current)
! read in a number, until 0 is entered
do
read*, number
if (number == 0) then
exit
end if
allocate (current) ! create new link
current%i = number
current%next => first ! point to previous link
first => current ! update head pointer
end do
! print the contents of the list
current => first ! point to beginning of the list
do
if (.not. associated (current)) then ! end of list reached
exit
end if
print*, current%i
current => current%next ! go the next link in the list
end do
end program linkedlist
current%i = pointer then it does NOT work. CAn anyone explain why not ? Why does
current%i = number work and current%i = pointer NOT work.