NextElement after DeleteElement

Just starting out? Need help? Post your questions and find answers here.
technicorn
Enthusiast
Enthusiast
Posts: 105
Joined: Wed Jan 18, 2006 7:40 pm
Location: Hamburg

NextElement after DeleteElement

Post 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
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
technicorn
Enthusiast
Enthusiast
Posts: 105
Joined: Wed Jan 18, 2006 7:40 pm
Location: Hamburg

Post 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!
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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.
quidquid Latine dictum sit altum videtur
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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! :)
technicorn
Enthusiast
Enthusiast
Posts: 105
Joined: Wed Jan 18, 2006 7:40 pm
Location: Hamburg

Post 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.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post 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
Post Reply