YG
BAN USERvoid skip_M_del_N(Node * node, unsigned int M, unsigned int N)
{
ASSERT(M + N); // M > 0 || N > 0 -- asking for an infinite loop
// ideally should also check if M and N each fit in a signed int
if ( N == 0 ) return; // nothing to delete -- we are done.
int count = M ? M : -N; // positive => skip; negative => delete. Won't be 0.
Node * last_skipped = 0;
while ( node ) {
if ( count > 0 ) { // more to skip
if ( --count == 0 ) {
count = -N; // flip to deleting
last_skipped = node; // remember where to re-link
}
node = node->next;
} else /* if ( count < 0 ) */ { // more to delete
Node * next = node->next;
delete node; // if necessary
node = next;
if ( last_skipped ) last_skipped->next = next;
if ( ++count == 0) count = M ? M : -N; // flip to skipping
}
} // while ( node )
}
Assuming that the bits from the stream are treated as added at the least-significant (right) end:
- YG March 27, 2012