Linked List

Bill Denney denney@seas.upenn.edu
Tue Oct 11 19:06:00 GMT 2005


Sorry if this is not the list for this type of question, I'm having 
trouble creating a linked list.  I feel like I'm missing something stupid. 
I'm losing the first element of the list when I run the attached code, I 
get the following:

  adding           1
  adding           2
  adding           3
  adding           4
  adding           5
  Entering Printnodes

And then it seg-faults.

What am I doing incorrectly?

Bill

-- 
"Man is so made that he can only find relaxation from one kind of labor by
taking up another."
   -- Anatole France
-------------- next part --------------
module typedefs
  implicit none
  type llstruct
     type(llstruct), pointer :: next => null()
     integer*4 :: num
  end type llstruct

end module typedefs

program ll
  use typedefs
  implicit none

  type(llstruct), pointer :: head, current

  integer*4 :: i

  nullify(head)
  nullify(current)

  do i = 1, 5
     write(*,*) "adding", i
     allocate(current)
     if (i == 1) then
        head => current
     end if
     current%num = i
     current => current%next
  end do

  call printnodes(head)

end program ll

subroutine printnodes(head)
  use typedefs
  implicit none

  type(llstruct), pointer :: head, current

  write(*,*) "Entering Printnodes"
  current => head

  do while (associated(current))
     write(*,*) current%num
     current => current%next
  end do
end subroutine printnodes


More information about the Fortran mailing list