From Programming Pearls: Column 12: Searching:
There are two code segments:
IntSetList(int maxelements, int maxval)
{ sentinel = head = new node(maxval, 0);
n = 0;
}
void report(int *v)
{ int j = 0;
for (node *p = head; p != sentinel; p = p->next)
v[j++] = p->val;
}
I’m curious how does the sentinel work in the report function?
If necessary, please see the complete source code.
Thanks,
sentinelis just the value that the last pointer in the list is expected to have; it’s the value that anextmember has when it doesn’t point to anything. It’s initialized to point to the first, “dummy” node in the list; new items are inserted at the beginning, so the sentinel drifts to the end.