Home > Archive > Fortran > November 2005 > deleting an element from the linked list
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
deleting an element from the linked list
|
|
| Millena 2005-11-21, 7:57 am |
| Hi.. really I need some help...
I trying to delet an element from a linked list, where the head of this
list is inside of an array of pointers...
the big problem, is because when i am deleting the current element I
verify that the element was really deleted, but if I access the head of
the list and considering that for this case I have just one element the
head of the list ISN'T empty... why is it happening????
here is some parts of my subroutine, perhaps someone can help me...
Subroutine flagbadtop
Implicit None
Type listI
Integer::ii0,ii1
Type(listI), Pointer :: nexti
End Type listI
Type jpointer
Type(listI),pointer::jbpt
End Type jpointer
!Is the array of pointers where is the head of the list that contain i0
and i1 and the pointer thar point to the next element
Type (jpointer), allocatable,dimension (:)::jbarray
!allocatin the array of pointers
ngtop = (B2-A2)/(dy/2.0d0)
Allocate(jbarray(ngtop),STAT=erra_jbarra
y)
If(erra_jbarray /= 0) Then
Write(0,*) "Program couldn't allocate space for jbarray"
Stop
End If
Do k = iuib,iuib + nl - 1, 6
ximage = lagX(k)-anint((lagX(k)-a1)/(b1-a1)-0.5)*(b1-a1)
yimage = lagY(k)-anint((lagY(k)-a2)/(b2-a2)-0.5)*(b2-a2)
ib = nint((ximage)/dx + 0.5)
jb = nint((yimage)/dy + 0.5)
i0 = ib - bzib
i1 = ib + bzib
Do j = jb-bzib,jb+bzib
jmax = max(j,jmax)
jmin = min(j,jmin)
dothis = .true.
curriaux => jbarray(j)%jbpt
do while (dothis)
!If the list is empty get a initial value
If (.not.associated(jbarray(j)%jbpt))then
allocate(jbarray(j)%jbpt)
jbarray(j)%jbpt%ii0 = i0
jbarray(j)%jbpt%ii1 = i1
jbarray(j)%jbpt%nexti => null() !nullyfing the next
pointer
Nullify(previous) !nullifying the
previous pointer (it is used when I have more than one element in the
list)
dothis = .false.
else
If (max(i0,curriaux%ii0) <= min(i1,curriaux%ii1))then
i0 = min(i0,curriaux%ii0)
i1 = max(i1,curriaux%ii1)
!***************************************
****************************************
!deallocating the element
oldnode => curriaux
previous%nexti => curriaux%nexti
curriaux => curriaux%nexti
deallocate(oldnode)
!***************************************
****************************************
!updating the current pointer. here is
necessary to return to the head of list calculus start from here
curriaux =>jbarray(j)%jbpt
!***************************************
****************************************
!***************************************
****************************************
!Testing if the head of the list is empty
write(0,*)jbarray(j)%jbpt%ii0,jbarray(j)
%jbpt%ii1
!if the first element that was in the head was (-5,-3) in the
jbarray(j)%jbpt%ii0 appear 0 and in the jbarray(j)%jbpt%ii1 appear -3
!for me the error is here... because was
necessary that the head of the list be null if I have just one element
in the list
!***************************************
****************************************
else if (i1 < curriaux%ii0)then
!Insering new element
allocate(newnode)
newnode%ii0 = i0
newnode%ii1 = i1
newnode%nexti => curriaux
previous%nexti => newnode
curriaux => newnode
else
!reading the next element from the list
If(.not.associated(curriaux%nexti))exit
!storing the previous element from the list
previous => curriaux
curriaux => curriaux%nexti
end if
end if
end do
end do
End do
End Subroutine flagbadtop
thanks in advance
Millena
| |
|
| Hi,
without looking too much in the code, if you have two pointers pointing
to the same element of the list, they do not become both null if you
deallocate one of the pointers. You have to nullify explicitly the
second pointer if you want to keep it up-to-date.
e.g.
integer, pointer :: i1,i2
allocate(i1)
i2=>i1
deallocate(i2)
is not enough (i.e. associated(i1) may return true). If you care about
i1 at that point, you need to add explicitly
nullify(i1)
Joost
Millena wrote:
> Hi.. really I need some help...
> I trying to delet an element from a linked list, where the head of this
> list is inside of an array of pointers...
>
> the big problem, is because when i am deleting the current element I
> verify that the element was really deleted, but if I access the head of
> the list and considering that for this case I have just one element the
> head of the list ISN'T empty... why is it happening????
>
> here is some parts of my subroutine, perhaps someone can help me...
>
>
> Subroutine flagbadtop
> Implicit None
>
> Type listI
> Integer::ii0,ii1
> Type(listI), Pointer :: nexti
> End Type listI
>
> Type jpointer
> Type(listI),pointer::jbpt
> End Type jpointer
>
> !Is the array of pointers where is the head of the list that contain i0
> and i1 and the pointer thar point to the next element
> Type (jpointer), allocatable,dimension (:)::jbarray
>
>
> !allocatin the array of pointers
> ngtop = (B2-A2)/(dy/2.0d0)
> Allocate(jbarray(ngtop),STAT=erra_jbarra
y)
> If(erra_jbarray /= 0) Then
> Write(0,*) "Program couldn't allocate space for jbarray"
> Stop
> End If
>
>
> Do k = iuib,iuib + nl - 1, 6
> ximage = lagX(k)-anint((lagX(k)-a1)/(b1-a1)-0.5)*(b1-a1)
> yimage = lagY(k)-anint((lagY(k)-a2)/(b2-a2)-0.5)*(b2-a2)
> ib = nint((ximage)/dx + 0.5)
> jb = nint((yimage)/dy + 0.5)
>
> i0 = ib - bzib
> i1 = ib + bzib
>
> Do j = jb-bzib,jb+bzib
> jmax = max(j,jmax)
> jmin = min(j,jmin)
> dothis = .true.
> curriaux => jbarray(j)%jbpt
> do while (dothis)
>
> !If the list is empty get a initial value
> If (.not.associated(jbarray(j)%jbpt))then
> allocate(jbarray(j)%jbpt)
> jbarray(j)%jbpt%ii0 = i0
> jbarray(j)%jbpt%ii1 = i1
> jbarray(j)%jbpt%nexti => null() !nullyfing the next
> pointer
> Nullify(previous) !nullifying the
> previous pointer (it is used when I have more than one element in the
> list)
> dothis = .false.
>
> else
> If (max(i0,curriaux%ii0) <= min(i1,curriaux%ii1))then
> i0 = min(i0,curriaux%ii0)
> i1 = max(i1,curriaux%ii1)
>
>
> !***************************************
****************************************
> !deallocating the element
> oldnode => curriaux
> previous%nexti => curriaux%nexti
> curriaux => curriaux%nexti
> deallocate(oldnode)
>
> !***************************************
****************************************
> !updating the current pointer. here is
> necessary to return to the head of list calculus start from here
> curriaux =>jbarray(j)%jbpt
>
> !***************************************
****************************************
>
>
> !***************************************
****************************************
> !Testing if the head of the list is empty
> write(0,*)jbarray(j)%jbpt%ii0,jbarray
(j)%jbpt%ii1
>
> !if the first element that was in the head was (-5,-3) in the
> jbarray(j)%jbpt%ii0 appear 0 and in the jbarray(j)%jbpt%ii1 appear -3
> !for me the error is here... because was
> necessary that the head of the list be null if I have just one element
> in the list
>
> !***************************************
****************************************
>
> else if (i1 < curriaux%ii0)then
> !Insering new element
> allocate(newnode)
> newnode%ii0 = i0
> newnode%ii1 = i1
>
> newnode%nexti => curriaux
> previous%nexti => newnode
> curriaux => newnode
>
> else
> !reading the next element from the list
> If(.not.associated(curriaux%nexti))exit
> !storing the previous element from the list
> previous => curriaux
> curriaux => curriaux%nexti
> end if
>
> end if
> end do
> end do
> End do
>
>
> End Subroutine flagbadtop
>
>
> thanks in advance
> Millena
| |
| Michael Metcalf 2005-11-21, 7:57 am |
|
"Millena" <millena.villar@gmail.com> wrote in message
news:1132577361.248213.87550@f14g2000cwb.googlegroups.com...
> Hi.. really I need some help...
> I trying to delet an element from a linked list, where the head of this
> list is inside of an array of pointers...
>
> Type listI
> Integer::ii0,ii1
> Type(listI), Pointer :: nexti => null()
> End Type listI
>
> Type jpointer
> Type(listI),pointer::jbpt => null()
> End Type jpointer
>
[snip]
>
> !If the list is empty get a initial value
> If (.not.associated(jbarray(j)%jbpt))then
I haven't ploughed right through your code, but I note the use of associated
on a pointer for which I can see no initialization. Pointers are created
with a status 'undefined' and cannot be tested in that state. They must
become 'disassociated' or 'associated' first. I recommend trying the two
modifications above as a start.
Regarfds,
Mike Metcalf
|
|
|
|
|