假设我们将您的队列实现为链表。我们可能有:
struct data_type;
struct node
{
node *next;
data_type item;
};
struct linked_list
{
node *pHead;
// ...
};
要清空链表,我们可以这样写:
linked_list *uq=...;
while (uq->pHead)
{
// unlink the first node from the list
node *tmp = uq->pHead;
uq->pHead = tmp->next;
// do something with that node
// ...
// deallocate the node
free(tmp);
}
现在假设我们并不真正关心可维护的代码,或者是懒惰的。相反,我们可能只是想出任何指针都会做的事情,并将“节点”的结构保留在我们的脑海中,然后写:
linked_list *uq=...;
while (uq->pHead)
{
// unlink the first node
char *tmp = uq -> pHead; // tmp points to the first 'node'
uq -> pHead = *(char**)tmp; // The first thing in a 'node' is a pointer to
// the next node.
// do something with 'tmp', the now unlinked node
data_type *item=(data_type*) ( ((char**)tmp) + 1 ); // after the 'next' pointer
// is the real data.
// ...
// free up the node
free(tmp);
}