[Fortran] 纯文本查看 复制代码
! 主程序
program T
implicit none
type node
real val
type(node),pointer :: P
end type
type(node),pointer :: pHead=>null(), pTail=>null(), pTemp=>null() !指针置空
integer istat, num, i
real rTemp
nullify(pHead, pTail, pTemp ) !断开关联(置空)
open(1,file='1.txt')
num = 0
do !循环读取数据
read(1,*,iostat=istat) rtemp
if(istat/=0) exit
num =num + 1
if( .NOT.associated(pHead) ) then !第一个数据
allocate(pHead) !分配空间
pTail => pHead !pHead、pTail均指向第一个数据
!nullify(pTail.P)
phead.val =rTemp
else
allocate(pTail.P)
pTail => pTail.P
!nullify(pTail.P)
pTail.val =rTemp
end if
end do
close(1)
if( associated(pTail) ) nullify(pTail.P) !如果有数据,最后一个节点的指针置空
print*,num
do i=1,num !循环输出
!if( .NOT.associated(pHead) ) exit
write(*,*) pHead.val
pTemp => pHead
pHead => pHead.P
deallocate(pTemp) !销毁数据
end do
pause
end program T