fortran用派生数据实现链表

本文介绍了一个使用Fortran实现的链表操作示例,包括读取文件数据到链表、遍历链表并输出数据的过程。示例中定义了链表节点类型,实现了链表初始化、插入和输出功能,展示了Fortran在数据结构处理上的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:
Module chainTableMod 
    implicit none 
    type :: real_value
        real :: value
        type( real_value ), pointer :: next
    end type 
    type( real_value ), pointer     :: head
    type( real_value ), pointer     :: tail
    type( real_value ), pointer     :: ptr
    character(len=20)               :: fname
    integer                         :: istat, fileid
    real                            :: temp
contains
    
    subroutine chainTable
        implicit none
        write( *,'(1x,a)' ) 'please input the filename: '
        read( *,'(a)' ) fname
        open( newunit = fileid, file = trim(fname), iostat = istat )
        fileopen: if ( istat == 0 ) then  !.. open file successfully
            input: do
                read( fileid, *, iostat = istat ) temp
                if ( istat /= 0 ) exit input
                if ( .not. associated(head) ) then
                    allocate( head, stat = istat )
                    tail       => head
                    tail%next  => null()
                    tail%value = temp
                else
                    allocate( tail%next, stat = istat )
                    tail       => tail%next
                    nullify( tail%next )
                    tail%value = temp
                end if
            end do input
        
            !.. write out the data
            ptr => head
            output: do
                if ( .not. associated(ptr) ) exit
                write( *,'(1x,f10.4)' ) ptr%value
                ptr => ptr%next
            end do output
        else  !.. open file failed
            write( *,'(1x,a,g0)' )  'file open failed--status = ', istat
        end if fileopen
        
    end subroutine chainTable
    
    
End module chainTableMod
    
Program main
    use chainTableMod
    implicit none
    call chainTable
End program main

需要用到的文件名为:input.dat
文件input.dat数据如下:
1.1
2.34
0.0
4.6
7.2
5.6
8.2
1.9
4.2
3.3
4.1

执行代码,结果如下:
1.1
2.34
0.0
4.6
7.2
5.6
8.2
1.9
4.2
3.3
4.1

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值