Catherine,
At 14:29 09-08-2000 -0700, Catherine Moroney wrote:
>I'm trying to write a simple linked list code in Fortran90 and I think
>I have it working except I don't seem to be able to deallocate it.
>If I print out the contents of the list after I try to deallocate it,
>I still get the same numbers back as I originally put in.
>
>I'm completely new to linked lists, so I'm probably making some simple
>logic error that I will be glad to have pointed out to me.
>
>Here's my test code (below). I'm running v7.3 of the SGI F90 compiler.
>The "sum" statement gives me 55 both times, before and after the
>deallocate.
>
>Catherine
>
>------------------------------------------------------------
>Catherine Moroney ph: (520) 626-5123
>Institute of Atmospheric Physics fax: (520) 621-6833
>University of Arizona [log in to unmask]
>P.O. Box 210081, Room 542
>(1118 East 4th Street)
>Tucson, AZ 85721-0081
>
This program may work on some systems, but it is not correct.
I don't have time to elaborate on it extensively.
I tried two systems: Salford FTN95 and Absoft f90.
Both gave different, but incorrect results.
Some remarks: (marked ![JvO] )
When I started on this subject a few years ago, I had some trouble
understanding the mechanism.
Just keep in mind that the algorithm must also work for an empty list and
for a list of just one node. So initialization and termination conditions
are essential.
DISCLAIMER: I did not check the complete correctness of the program with my
suggested corrections/modifications.
>> PROGRAM LIST
>>
>> IMPLICIT NONE
>>
>> TYPE LLIST
>> INTEGER :: I
>> TYPE (LLIST), POINTER :: NEXT
>> END TYPE LLIST
>>
>> TYPE (LLIST), POINTER :: BEGIN,ILIST,TMP
>> INTEGER :: I,ISUM
>>
>>
>> allocate(begin)
>> ilist => begin
>> nullify(ilist%next)
>>
>>! POPULATE LIST
>>
>> do i=1,10
>> ilist%i = i
>> allocate( ilist%next )
>> ilist => ilist%next
>> end do
>> nullify(ilist%next)
>>
>>! SUM ELEMENTS IN LIST, SUM SHOULD BE 55.
>>
>> isum = 0
>> ilist => begin
! [JvO] Consider a list of just one node:
>>! [JvO] do while (associated(ilist)) ! [JvO]
do while (associated(ilist%next)) ! [JvO] modified.
>> isum = isum + ilist%i
>> ilist => ilist%next
>> end do
>> write(*,*) 'sum of elements = ',isum
>>
>>! DEALLOCATE LIST ONE ELEMENT AT A TIME
>>
>> ilist => begin
>> do while (associated(ilist%next))
>> tmp => ilist%next
>> write(*,*) 'deallocating element = ',ilist%i
>> deallocate(ilist)
![JvO] After the first deallocation "begin" is dangling, association status
![JvO] undefined.
![JvO]
![JvO] So at least add the statement here:
>> ilist => tmp
>> end do
nullify (begin) ! To be sure !
>>
>>! CHECK FOR STATUS OF LIST BY SUMMING ELEMENTS AGAIN
>>
>> isum = 0
>> ilist => begin
>> do while (associated(ilist))
>> isum = isum + ilist%i
>> ilist => ilist%next
>> end do
>> write(*,*) 'sum of elements = ',isum
>>
>>STOP
>>END
--
Meilleures Salutations,
Kindest Regards,
/---
Jan van Oosterwijk
Computing Centre
Delft University of Technology
Postbus 354
2600 AJ Delft
Netherlands / Pays-Bas
mailto:[log in to unmask]
Phone: +31 15 278 50 17
Fax: +31 15 278 37 87
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|