smart 发表于 2015-11-10 12:44:07

彭书指针链表删除插入问题

彭国伦书中第298页   程序中插入数据和删除数据程序代码   我去掉注释部分后程序依然能很好地运行   去掉后不知道逻辑上是否有错误   注释部分在程序中有什么关键的不可或缺的作用吗?? 彭国伦的代码是这样的:

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
subroutineinsitem(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
注释掉后程序依然能够很好地运行我把注释地方去掉   逻辑上有什么错误吗??

fcode 发表于 2015-11-10 18:06:05

请你把去除和插入的代码改为:
write(*,*) "去掉"
call delitem(head%next%next%next)
call outputlist(head)
write(*,*) "插入"
allocate(item)
item%i=30
call insitem(head%next%next%next,item,.false.)
call outputlist(head)你再试试注释和不注释有什么区别?

fcode 发表于 2015-11-10 18:22:14

配一张图给你,易于理解。

smart 发表于 2015-11-10 23:22:05

膜拜   太详细了透彻   谢谢你   

smart 发表于 2015-11-10 23:25:19

fcode 发表于 2015-11-10 18:22
配一张图给你,易于理解。

详细透彻谢谢楼主
页: [1]
查看完整版本: 彭书指针链表删除插入问题