各ノードの割り当てを手動で解除する必要があります。これは、スタイルのような "オブジェクト指向"が役立つところです。
module LinkedListModule
implicit none
private
public :: LinkedListType
public :: New, Delete
public :: Append
interface New
module procedure NewImpl
end interface
interface Delete
module procedure DeleteImpl
end interface
interface Append
module procedure AppendImpl
end interface
type LinkedListType
type(LinkedListEntryType), pointer :: first => null()
end type
type LinkedListEntryType
integer :: data
type(LinkedListEntryType), pointer :: next => null()
end type
contains
subroutine NewImpl(self)
type(LinkedListType), intent(out) :: self
nullify(self%first)
end subroutine
subroutine DeleteImpl(self)
type(LinkedListType), intent(inout) :: self
if (.not. associated(self%first)) return
current => self%first
next => current%next
do
deallocate(current)
if (.not. associated(next)) exit
current => next
next => current%next
enddo
end subroutine
subroutine AppendImpl(self, value)
if (.not. associated(self%first)) then
allocate(self%first)
nullify(self%first%next)
self%first%value = value
return
endif
current => self%first
do
if (associated(current%next)) then
current => current%next
else
allocate(current%next)
current => current%next
nullify(current%next)
current%value = value
exit
endif
enddo
end subroutine
end module
注意:これは深夜過ぎで、ブラウザウィンドウでのコーディングが本当に好きではありません。このコードは機能しない可能性があります。それは単なるレイアウトです。この
program foo
use LinkedListModule
type(LinkedListType) :: list
call New(list)
call Append(list, 3)
call Delete(list)
end program
よう
使用して、私がFortranでこれをしなかったので、長い時間がかかったが、私はあなたが手動で解放する必要があり、かなり確信しています。頭だけの割り当てを解除すると、参照が失われてメモリリークが発生します。 – ChrisF
はい。私はそれをかなり恐れていました。私は言わなくてはいけません、私は問題を抱えています。フレーズは何ですか、私自身のガベージコレクションを巻き込んでいますか? – EMiller
メモリー管理Fortranは実装できません。 –