* Parameter "max" is not used.
* Code contains magic "30".
* The condition does not return true for values in range.
* The condition does return true for values not in range.
* What if cursor it within deleted range?
Do the operation in phases: 1. Find beginning or range2. Find end of range
3. Do removal, if necessary, and handle special cases
Edit: Different meaning of "range". To me, a range is a set of consecutive elements. Your case is a remove_if, where the predicate has a value range.
you don't check the value of cursor, you may want to check it too if you want to erase ALL elements in the range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
NodeType *prev;
do
{
if (temp->info >= min && temp->info <= max) // changed max
{
NodeType *garbage = temp;
prev = temp->next;
delete garbage;
}
else
{ // added bracket
prev = temp;
temp = temp->next;
} // added bracket
} while (temp->next != cursor) //traverse whole node till you reach cursor, also checks cursor
these are all just little things but i think they add up, I didn't run through the code in detail though, I just assumed that your algorithm will work but that u forgot some little things
Ok so I sketched out my code on paper to see if the iteration follows correctly and if I was linking and delinking properly.
I found the source of my problem to come from the de-link processes. I need to re-link the node previous to the cursor, back to the cursor. For example, since my last node value is 15, and 15 is between 10-30, it gets deleted. Thus, I have to re-link the node before that to the cursor.
How about considering the temp->next for delete, rather than the temp?
Unrelated notes about your code:
* Lines 19, 27 and 29 are unnecessary.
* C++11: use nullptr, not NULL or 0.
* The temp is used only within the scope of else (lines 11-27), so it should be declared there.
* Line 7: if list is empty but cursor is not nullptr, then your other methods are not consistent.
Let us go over it part by part
you set garbage to temp
prev to next temp
and delete garbage which results in deleting temp
then you ask if temp-> next == cursor, temp is deleted so this will have problems.
also keep in mind that you may end up delete cursor
auto temp = cursor; // this is where we start
// usual case:
auto dead = temp->next;
if ( dead must die )
{
temp->next = dead->next; // remove node from list
delete dead;
}
else
{
temp = dead; // let live and advance temp
}
// That doesn't cover the special cases
// 1. The list has only one node (left): cursor->next == cursor
// The list may become empty and then: cursor = nullptr
// 2. The list has more than one, and we are looking at the last: temp->next == cursor
// Whether the dead must die or not, this is the end of iteration
// If the dead must die, then and only then the cursor updates to next (remaining) node.
Hm Keskiverto, I kind of understand what you are saying. Are you stating that the iteration is not enough because it will stop the loop once it deletes the first node thats between min and max?
This is what happened in my code, it only deleted 1 node but nothing else.
Currently my node looks like:
20 - 10 - 80 - 60 - 30 - 90 - 15 - back to 20
and after the function call:
20 - 80 - 60 - 30 - 90 - 15 - back to 20
That (only 1 deleted) should be elementary:
Line 3 does temp=cursor
Line 27 sees that temp==cursor You never change the temp.
The Gamer2015 version is quite opposite:
Lets have list:
42, 20, 10, 16.
* temp is at 42
* next is at 20. Will be removed.
* After line 15 the list is 42, 10, 16 and temp is still at 42
* Line 16 moves temp to 10
### next iteration
* next is at 16. Will be removed. The node 10 was skipped. The temp moves even when it should not.