Page 1 of 1
NextElement after DeleteElement
Posted: Sun Jan 04, 2009 3:34 pm
by technicorn
Run this an you see that not every second element gets deleted as one would expect:
Code: Select all
#ListSize = 20
OpenConsole()
NewList l1.l()
For n = 1 To #ListSize
AddElement(l1())
l1() = n
Next n
ResetList(l1())
PrintN("Before deletetion")
n = 0
While NextElement(l1())
PrintN(RSet(Str(n), 3) + ": " + Str(l1()))
n + 1
Wend
PrintN("")
SelectElement(l1(), 0)
For n = 1 To #ListSize >> 1
DeleteElement(l1(), 1) ; Delete current element, make next element current element
NextElement(l1()) ; Should skip one element (but it doesn't)
; NextElement(l1())
Next n
ResetList(l1())
PrintN("After deletetion: " + Str(ListSize(l1())))
n = 0
While NextElement(l1())
PrintN(RSet(Str(n), 3) + ": " + Str(l1()))
n + 1
Wend
PrintN("")
PrintN("Press ENTER key"): Input()
If you uncomment the second NextElement than it works.
Tested on Windows XP in 32 bit mode
Posted: Sun Jan 04, 2009 4:18 pm
by Trond
SelectElement(l1(), 0)
Not related to the problem, but FirstElement() will be much faster.
Apart from that, there doesn't seem to be any bugs here, except in your code.
First, DeleteElement() deletes the first element. Now, element "2" becomes the current one, since it's the first in the list. NextElement() goes to the next element (can be verified by debug output of l1()).
Then, on the next loop iterations, DeleteElement() works normally (as without the flag), since it doesn't delete the first element of the list. It deletes the second element, sets the previous element (the first one) as the current, then nextelement() switches back to the second element again.
Posted: Sun Jan 04, 2009 4:23 pm
by Trond
You can delete every other element like this:
Code: Select all
LastElement(l1())
For n = 1 To #ListSize >> 1
PreviousElement(l1())
DeleteElement(l1())
Next n
Posted: Sun Jan 04, 2009 5:27 pm
by technicorn
Normaly I try to avoid sarcastic comments on other, but not this time.
1. I know that using FirstElement() would be faster,
I programmed a linked list library myself which has 1500+ lines of PB code,
and if you read some of my posts, you should now that I know what im programming (most of the time anyway)
So no need to point out this, as if I use PB for about a day.
2. Would you please RTFM about DeleteElement()
The flag says to go to the element that follow the deleted element.
So doing a DeleteElement() and than do NextElement()
must skip an element!
3. Where's there a bugs in my program?????
[Edit]
New code to proof that this is a bug:
Code: Select all
#ListSize = 20
OpenConsole()
NewList l1.l()
For n = 1 To #ListSize
AddElement(l1())
l1() = n
Next n
ResetList(l1())
PrintN("Before deletetion")
n = 0
While NextElement(l1())
PrintN(RSet(Str(n), 3) + ": " + Str(l1()))
n + 1
Wend
PrintN("")
SelectElement(l1(), 0)
For n = 1 To #ListSize >> 1
PrintN("Deleting element " + Str(ListIndex(l1())) + ": " + Str(l1()))
DeleteElement(l1(), 1) ; Delete current element, make next element current element
PrintN("Actual element " + Str(ListIndex(l1())) + ": " + Str(l1()))
NextElement(l1()) ; Should skip one element (but it doesn't)
PrintN("Next element " + Str(ListIndex(l1())) + ": " + Str(l1()))
; NextElement(l1())
Next n
PrintN("")
ResetList(l1())
PrintN("After deletetion: " + Str(ListSize(l1())))
n = 0
While NextElement(l1())
PrintN(RSet(Str(n), 3) + ": " + Str(l1()))
n + 1
Wend
PrintN("")
PrintN("Press ENTER key"): Input()
Have a look at the index and values that should be deleted and what really gets deleted!
Posted: Sun Jan 04, 2009 6:06 pm
by freak
The flag only controls what happens if you delete the first element of the list. If you delete any other element, it has no effect.
Posted: Sun Jan 04, 2009 6:30 pm
by Trond
And I repeat:
My first post wrote:Then, on the next loop iterations, DeleteElement() works normally (as without the flag), since it doesn't delete the first element of the list. It deletes the second element, sets the previous element (the first one) as the current, then nextelement() switches back to the second element again.
Perhaps you should take a look at the manual again. The flag
only changes the behavior of DeleteElement() when you delete the
first element.
Manual wrote:But, if the parameter 'Flags' is set to 1 and the first element is deleted, the new current element will be the second one.
Have a nice day, Mr. Sarcasm!

Posted: Sun Jan 04, 2009 6:59 pm
by technicorn
Manual wrote:
But, if the parameter 'Flags' is set to 1 and the first element is deleted, the new current element will be the second one.
Than this should be stated more clearly in the manual that the flag only changes the behaviour on deleting the first element,
and there should be a flag value of 2 for having the effect as I would expect from reading the manual.
Posted: Sun Jan 04, 2009 7:19 pm
by rsts
Manual wrote:
But, if the parameter 'Flags' is set to 1 and the first element is deleted, the new current element will be the second one. This flag ensure than there will be always a valid current element after a delete. If there was only one element in the list when you deleted it then you are left with no current element!
If you read a little further, it becomes clear in intent and use. If you're further along in the list you would not have to worry about no current element. It's also pointed out in the example included in the manual.
cheers