Andrew Y..
12
这看起来像TAILQ_*的一个很好的候选人?
#include
"man queue"将提供更多细节 - 那里有简单的列表,尾部队列和循环队列.这些是您需要在自己的结构上固定的宏,当然不是类.
你的场景的代码看起来像这样(我应该让add_to_queue返回一些代码来检查错误,并避免全局变量,但希望在这个例子中我会被原谅):
#include
#include
#include
TAILQ_HEAD(tailhead, entry) head;
struct entry {
char c;
TAILQ_ENTRY(entry) entries;
};
void add_to_queue(char ch) {
struct entry *elem;
elem = malloc(sizeof(struct entry));
if (elem) {
elem->c = ch;
}
TAILQ_INSERT_HEAD(&head, elem, entries);
}
int main(int argc, char *argv[]) {
char ch = 'A';
int i;
struct entry *elem;
TAILQ_INIT(&head);
for (i=0; i<4; i++) {
add_to_queue(ch);
ch++;
add_to_queue(ch);
elem = head.tqh_first;
TAILQ_REMOVE(&head, head.tqh_first, entries);
free(elem);
}
exit(0);
}