彭国伦书中第298页 程序中插入数据和删除数据程序代码 我去掉注释部分后程序依然能很好地运行 去掉后不知道逻辑上是否有错误 注释部分在程序中有什么关键的不可或缺的作用吗?? 彭国伦的代码是这样的:
[Fortran] 纯文本查看 复制代码
module linklist
implicit none
type::datalink
integer::i
type(datalink),pointer::prev
type(datalink),pointer::next
end type datalink
contains
subroutine outputlist(list)
implicit none
type(datalink),pointer::list,p
p=>list
do while(associated(p))
write(*,*) p%i
p=>p%next
end do
return
end subroutine
subroutine delitem(item)
implicit none
type(datalink),pointer::item
type(datalink),pointer::prev,next
prev=>item%prev
next=>item%next
deallocate(item)
!if(associated(prev)) prev%next=>next
!if(associated(next)) next%prev=>prev
item=>next
return
end subroutine
subroutine insitem(pos,item,after)
implicit none
type(datalink),pointer::pos,item
logical::after
if(after) then
item%next=>pos%next
item%prev=>pos
!if(associated(pos%next)) then
!pos%next%prev=>item
!end if
pos%next=>item
else
item%next=>pos
item%prev=>pos%prev
if(associated(pos%prev)) then
pos%prev%next=>item
end if
pos%prev=>item
end if
return
end subroutine
end module
program ex1015
use linklist
implicit none
type(datalink),pointer::head,forlatter
type(datalink),pointer::item,p
integer,parameter::s=5
integer::i,n,error
allocate(head)
head=datalink(1,null(),null())
p=>head
do i=2,s
allocate(p%next,stat=error)
if(error/=0) then
write(*,*) "Out of memory"
stop
end if
P%next=datalink(i,p,null())
p=>p%next
end do
write(*,*) "去掉"
call delitem(head%next%next%next%next)
call outputlist(head)
write(*,*) "插入"
allocate(item)
item%i=30
call insitem(head%next%next%next,item,.true.)
call outputlist(head)
stop
end program
注释掉后程序依然能够很好地运行 我把注释地方去掉 逻辑上有什么错误吗??
|