Fortran Coder

查看: 10605|回复: 4
打印 上一主题 下一主题

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

[复制链接]

3

帖子

1

主题

0

精华

新人

F 币
14 元
贡献
8 点
跳转到指定楼层
楼主
发表于 2015-11-10 12:44:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
3F 币
彭国伦书中第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

分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

沙发
发表于 2015-11-10 18:06:05 | 只看该作者
请你把去除和插入的代码改为:
[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)
你再试试注释和不注释有什么区别?
回复

使用道具 举报

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

板凳
发表于 2015-11-10 18:22:14 | 只看该作者
配一张图给你,易于理解。

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

Sample Flowchart Template.png
回复

使用道具 举报

3

帖子

1

主题

0

精华

新人

F 币
14 元
贡献
8 点
地板
 楼主| 发表于 2015-11-10 23:22:05 | 只看该作者
膜拜   太详细了  透彻   谢谢你   
回复

使用道具 举报

3

帖子

1

主题

0

精华

新人

F 币
14 元
贡献
8 点
5#
 楼主| 发表于 2015-11-10 23:25:19 | 只看该作者
fcode 发表于 2015-11-10 18:22
配一张图给你,易于理解。

详细透彻  谢谢楼主
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-2 05:36

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表