1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
template<class Elem> class Link {
private:
void update_me()
{
if (!next) {
next = this;
} else {
Link *current = next;
while (current->next != next) {
current = current->next;
}
// we found a node which already loop on next, so we must insert
// us at that very point.
current->next = this;
}
}
void delete_me()
{
if (next == this)
return; // the list is going to die with us
Link *prev = NULL;
Link *current = next;
while (current) {
if (current == this) {
// we just hit the "end" of the list, i.e. the point
// where we go back to us. We need to bypass
// us, and then we're done.
prev->next = next;
break;
}
current = current->next;
}
}
public :
Elem element ;
Link * next ;
Link (const Elem& elemval, Link* nextval =NULL)
{
element =elemval ; next =nextval ;
update_next();
}
Link( Link*nextval =NULL)
{
next= nextval ;
update_next();
}
~Link()
{
delete_me();
}
... |