【旧代码】fortran中的指针实现链表的代码

我不喜欢fortran,

奇怪的词法规则(竟然不用空格分开token),

io操作竟然是语言的一部分(这种非本质的东西像C那样用库来扩展多好)

。。。

 1 program link
 2 implicit none
 3 type node
 4     character(20) name
 5     integer id
 6     integer score
 7     type(node),pointer ::next
 8 end type
 9 
10 integer,parameter::num=3
11 type(node),pointer ::students
12 
13 call init_all(students,num)
14 call show_all(students)
15 print*,"now,we add a student"
16 call add_node(students)
17 call show_all(students)
18 print*,"now,we delete the student"
19 call del_node(students)
20 call show_all(students)
21 
22 
23 contains 
24 subroutine add_node(pos)
25 type(node),pointer ::pos,tmp
26 allocate(tmp)
27 tmp%name="abc"
28 tmp%id=1
29 tmp%score=100
30 print*,"please input name,id,end score of one students"
31 read*,tmp%name,tmp%id,tmp%score
32 if(associated(pos%next)) then
33 tmp%next=>pos%next
34 pos%next=>tmp
35 else 
36 nullify(tmp%next)
37 pos%next=>tmp
38 end if
39 end subroutine
40 
41 subroutine del_node(pos)
42 type(node),pointer ::pos,next
43 next=>pos%next
44 if(associated(next%next)) then
45 pos%next=>next%next
46 deallocate(next)
47 else
48 nullify(pos%next)
49 deallocate(next)
50 end if
51 end subroutine
52 
53 subroutine show_all(pos)
54 type(node),pointer ::pos,tmp
55 integer ::cnt
56 cnt=1
57 print*,"all students are:"
58 tmp=>pos
59 do while(associated(tmp))
60 print*,cnt,"th ",tmp%name,tmp%id,tmp%score
61 tmp=>tmp%next
62 cnt=cnt+1
63 end do
64 end subroutine
65 
66 subroutine init_all(pos,num)
67 type(node),pointer ::pos,tmp,walk
68 integer num
69 integer i
70 
71 allocate(tmp)
72 nullify(tmp%next)
73 call add_node(tmp)
74 pos=>tmp%next
75 deallocate(tmp)
76 walk=>pos
77 do i=1,num-1
78 call add_node(walk)
79 walk=>walk%next
80 end do
81 end subroutine
82 
83 end program link

program link implicit none type node character(20) name integer id integer score type(node),pointer ::next end type integer,parameter::num=3 type(node),pointer ::students call init_all(students,num) call show_all(students) print*,"now,we add a student" call add_node(students) call show_all(students) print*,"now,we delete the student" call del_node(students) call show_all(students) contains subroutine add_node(pos) type(node),pointer ::pos,tmp allocate(tmp) tmp%name="abc" tmp%id=1 tmp%score=100 print*,"please input name,id,end score of one students" read*,tmp%name,tmp%id,tmp%score if(associated(pos%next)) then tmp%next=>pos%next pos%next=>tmp else nullify(tmp%next) pos%next=>tmp end if end subroutine subroutine del_node(pos) type(node),pointer ::pos,next next=>pos%next if(associated(next%next)) then pos%next=>next%next deallocate(next) else nullify(pos%next) deallocate(next) end if end subroutine subroutine show_all(pos) type(node),pointer ::pos,tmp integer ::cnt cnt=1 print*,"all students are:" tmp=>pos do while(associated(tmp)) print*,cnt,"th ",tmp%name,tmp%id,tmp%score tmp=>tmp%next cnt=cnt+1 end do end subroutine subroutine init_all(pos,num) type(node),pointer ::pos,tmp,walk integer num integer i allocate(tmp) nullify(tmp%next) call add_node(tmp) pos=>tmp%next deallocate(tmp) walk=>pos do i=1,num-1 call add_node(walk) walk=>walk%next end do end subroutine end program link

我很少讨厌某种技术,坦诚的说,关于fortran,是有其他因素参合进来了。

转载于:https://www.cnblogs.com/windydays/archive/2011/11/15/2298099.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值