3

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

 2 years ago
source link: https://byronhe.com/post/2011/11/15/linked-list-in-fortran-with-pointer/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

2011-11-15

我不喜欢fortran,

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

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

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,是有其他因素参合进来了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK