#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/queue.h> struct entry { int id; TAILQ_ENTRY(entry) entries; }; int main(void) { int i; struct entry *t, *p; TAILQ_HEAD(tailhead, entry) head; struct tailhead *headp; TAILQ_INIT(&head); for (i = 0; i < 10; i++) { t = (struct entry*)malloc(sizeof(struct entry)); memset(t, 0, sizeof(t)); t->id = i; TAILQ_INSERT_TAIL(&head, t, entries); } for (p = head.tqh_first; p != NULL; p = p->entries.tqe_next) { printf("%08x %d\n", p, p->id); } t = (struct entry*)malloc(sizeof(struct entry)); memset(t, 0, sizeof(t)); t->id = 100; printf("Push new entry after 4th element\n"); p = head.tqh_first; for (i = 0; i < 3; i++) { p = p->entries.tqe_next; } TAILQ_INSERT_AFTER(&head, p, t, entries); for (p = head.tqh_first; p != NULL; p = p->entries.tqe_next) { printf("%08x %d\n", p, p->id); } printf("Pop head entry\n"); TAILQ_REMOVE(&head, head.tqh_first, entries); for (p = head.tqh_first; p != NULL; p = p->entries.tqe_next) { printf("%08x %d\n", p, p->id); } return 0; }実行結果
>queue.exe 006b2580 0 006b2590 1 006b25a0 2 006b25b0 3 006b25c0 4 006b25d0 5 006b25e0 6 006b25f0 7 006b2600 8 006b2610 9 Push new entry after 4th element 006b2580 0 006b2590 1 006b25a0 2 006b25b0 3 006c2628 100 006b25c0 4 006b25d0 5 006b25e0 6 006b25f0 7 006b2600 8 006b2610 9 Pop head entry 006b2590 1 006b25a0 2 006b25b0 3 006c2628 100 006b25c0 4 006b25d0 5 006b25e0 6 006b25f0 7 006b2600 8 006b2610 9
C++ での queue class の使い方 (queue)
#include <iostream> #include <queue> using namespace std; int main(void) { int i; queue<int> que; for (i = 0; i < 10; i++) { que.push(i); } cout << "size=" << que.size() << endl; while (!que.empty()) { cout << que.front() << " "; que.pop(); } cout << endl; return 0; }実行結果
>queue2.exe size=10 0 1 2 3 4 5 6 7 8 9
0 件のコメント:
コメントを投稿