How to remove even elements from list without using a counter?
How to remove even elements from list without using a counter?
As was mentioned in a similar thread recently, a boolean value is one way. Just a flip-flop switch.
If you're asking how to do this without an i variable to scan through the subscript values, then I have no idea. You could use memory addresses (pointers), but that's just another form of a counter.Code:pseudo-code: i = even = 0 while (array[i] has another element) { if (even = 0) { zero out that element of the array even = 1 else even = 0 } increment i }
I am talking about pointer lists
In a pedantic sense you could just start at one end of the list and free every other element. I assume odd items are first, but for example...
You might need to edit that if you need to look at node data to figure out what's even or odd.Code:node *walk; node *del; while( walk != NULL && walk->next != NULL && walk->next->next != NULL ) { del = walk->next->next; walk = del->next; delete( del ); }