Fortran Coder

标题: 彭书指针链表删除插入问题 [打印本页]

作者: smart    时间: 2015-11-10 12:44
标题: 彭书指针链表删除插入问题
彭国伦书中第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

注释掉后程序依然能够很好地运行  我把注释地方去掉   逻辑上有什么错误吗??

彭国伦指针问题.zip

22.86 KB, 下载次数: 2


作者: fcode    时间: 2015-11-10 18:06
请你把去除和插入的代码改为:
[Fortran] 纯文本查看 复制代码
  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
配一张图给你,易于理解。

Sample Flowchart Template.png (19.89 KB, 下载次数: 445)

Sample Flowchart Template.png

作者: smart    时间: 2015-11-10 23:22
膜拜   太详细了  透彻   谢谢你   
作者: smart    时间: 2015-11-10 23:25
fcode 发表于 2015-11-10 18:22
配一张图给你,易于理解。

详细透彻  谢谢楼主




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2