#include using std::cout; using std::endl; struct T { T*prev; T*next; int seq; }; void weed_list( T* &head, bool (*condition)(T*) ) { for ( T** dp = &head ; *dp ; ) { T* c = *dp; T* *n = &c->next; if (condition(c)) { if (*n) (*n)->prev = c->prev; *dp = *n; cout << "deleting " << c->seq << " " << (long)c << endl; } else { dp = n; } } } extern T a,b,c,d,e; T a = { 0, &b, 0 }; T b = { &a, &c, 1 }; T c = { &b, &d, 2 }; T d = { &c, &e, 3 }; T e = { &d, 0, 4 }; T* h = &a; bool prune(T*c) { return c->seq % 3 == 0; } int main() { cout << "starting with..." << endl; for (T* c = h; c;c=c->next) cout << "starter " << c->seq << " " << (long)c << endl; cout << endl; cout << "pruning..." << endl; weed_list(h, prune); cout << endl; cout << "keeping..." << endl; for (T* c = h; c;c=c->next) cout << "keeping " << c->seq << " " << (long)c << endl; }