实现:
#ifndef HO_QUEUE_H
#define HO_QUEUE_H
#include
#undef offsetof
struct queue_head {
struct queue_node *head;
struct queue_node *tail;
};
struct queue_node {
struct queue_node *next;
};
#define QUEUE_HEAD_INIT {.head = NULL, .tail = NULL}
#define INIT_QUEUE_HEAD(ptr) do {\
(ptr)->head = NULL;(ptr)->tail = NULL; \
} while (0)
#define offsetof(type, member) ((size_t)&((type *)0)->member)
#define queue_entry(ptr, type, member) (type *)((char *)ptr - offsetof(type, member))
static inline void queue_put(struct queue_node *n, struct queue_head *h) {
n->next = NULL;
if (!h->head) {
h->head = h->tail = n;
} else {
h->tail->next = n;
h->tail = n;
}
}
static inline struct queue_node *queue_get(struct queue_head *h) {
struct queue_node *t = h->head;
h->head = h->head ? h->head->next: NULL;
return t;
}
static inline int queue_empty(struct queue_head *h) {
return !h->head;
}
#define queue_for_each(pos, h) \
for(pos = (h)->head; pos; pos = pos->next)
#endif
如何使用:
#include
#include
#include
#include "queue.h"
struct queue_node2 {
void *data;
struct queue_node queue;
};
int main() {
struct queue_head head = QUEUE_HEAD_INIT;
struct queue_node *pos;
struct queue_node2 *tmp;
tmp = malloc(sizeof(struct queue_node2));
tmp->data = strdup("aaaaaaaaaaaaaaa");
queue_put(&tmp->queue, &head);
tmp = malloc(sizeof(struct queue_node2));
tmp->data = strdup("bbbbbbbbbbbbbbb");
queue_put(&tmp->queue, &head);
tmp = malloc(sizeof(struct queue_node2));
tmp->data = strdup("ccccccccccccccc");
queue_put(&tmp->queue, &head);
tmp = malloc(sizeof(struct queue_node2));
tmp->data = strdup("ddddddddddddddd");
queue_put(&tmp->queue, &head);
#if 0
queue_for_each(pos, &head) {
tmp = queue_entry(pos, struct queue_node2, queue);
printf("%s\n", (char *)tmp->data);
}
#endif
while (!queue_empty(&head)) {
pos = queue_get(&head);
tmp = queue_entry(pos, struct queue_node2, queue);
printf("%s\n", (char *)tmp->data);
free(tmp->data);
free(tmp);
}
return 0;
}