No body thinks of these and Fortran in the same breadth

Many people are surprised when you say, you can do type definition, pointers and dynamic memory allocation in Fortran. These are in Fortran since the time Fortran 90 was released, 15+ years back. Now Fortran 2003 has all features of object oriented programming language.

But here i present an example of linked list in Fortran 90.

If you study the example carefully, you will learn 3 new concepts in Fortran.

  • Type definition.
  • Pointers
  • Dynamic memory allocation.

No body thinks of these and Fortran in the same breadth but they are very much part of Fortran.

 
program linkedlist
implicit none
 
type node
real (kind=8) :: x,y,z
type(node),pointer :: next
end type
 
type(node),pointer :: list, first
integer :: iunit,icnt
real(kind =8),allocatable :: eudis(:)

iunit=10
open(iunit,file=’coordsdata.dat’)
allocate(list)
nullify(list%next)
first => list
icnt=1
do
   read(iunit,'(3f6.2)’,end=100) list%x,list%y,list%z
   icnt=icnt+1
   allocate(list%next)
   list=>list%next
   nullify(list%next)
end do
100 close(iunit)
 
allocate(eudis(icnt))
list => first
icnt=1
do while(associated(list%next))
   eudis(icnt)=(list%x**2+list%y**2+list%z**2)**0.5
   print *, icnt , eudis(icnt)
   icnt=icnt+1
   list => list%next
end do
 
end program

One thought on “No body thinks of these and Fortran in the same breadth

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s