主题:[讨论]指针问题!
下面这个程序在调试过程中,
1.执行完call delitem()后,指针P会随着item而变化,明明没有指向item啊!
2.插入(1)行,结果显示都是5,可是我再插入(2)行,运行程序就会出错,怎么回事啊?
希望高手指点下,谢谢!
---------------------------------------------------------
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,p1
p1=>list
do while(associated(p1))
write(*,*) p1%i
p1=>p1%next
end do
end subroutine outputlist
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
end subroutine delitem
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
end subroutine insitem
end module linklist
program test
use linklist
implicit none
type(datalink),pointer::head
type(datalink),pointer::item
type(datalink),pointer::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
[color=FF0000]!write(*,*) p%i,head%next%next%next%next%i !(1)[/color]
[color=FF0000]!deallocate(p) !(2)[/color]
write(*,*) '拿掉第三条数据'
[color=FF0000]call delitem(head%next%next)[/color]
call outputlist(head)
write(*,*) '插入新的第三条数据'
allocate(item)
item%i=30
call insitem(head%next,item,.true.)
call outputlist(head)
end program test
1.执行完call delitem()后,指针P会随着item而变化,明明没有指向item啊!
2.插入(1)行,结果显示都是5,可是我再插入(2)行,运行程序就会出错,怎么回事啊?
希望高手指点下,谢谢!
---------------------------------------------------------
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,p1
p1=>list
do while(associated(p1))
write(*,*) p1%i
p1=>p1%next
end do
end subroutine outputlist
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
end subroutine delitem
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
end subroutine insitem
end module linklist
program test
use linklist
implicit none
type(datalink),pointer::head
type(datalink),pointer::item
type(datalink),pointer::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
[color=FF0000]!write(*,*) p%i,head%next%next%next%next%i !(1)[/color]
[color=FF0000]!deallocate(p) !(2)[/color]
write(*,*) '拿掉第三条数据'
[color=FF0000]call delitem(head%next%next)[/color]
call outputlist(head)
write(*,*) '插入新的第三条数据'
allocate(item)
item%i=30
call insitem(head%next,item,.true.)
call outputlist(head)
end program test