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
22.86 KB, 下载次数: 2
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)
Sample Flowchart Template.png (19.89 KB, 下载次数: 613)
fcode 发表于 2015-11-10 18:22
配一张图给你,易于理解。
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) | Powered by Discuz! X3.2 |