diff --git a/klist.h b/klist.h index adc3db1e..fa1b8760 100644 --- a/klist.h +++ b/klist.h @@ -108,13 +108,16 @@ return &q->data; \ } \ SCOPE int kl_shift_##name(kl_##name##_t *kl, kltype_t *d) { \ - kl1_##name *p; \ if (kl->head->next == 0) return -1; \ --kl->size; \ - p = kl->head; kl->head = kl->head->next; \ + kl1_##name *p = kl->head; kl->head = kl->head->next; \ if (d) *d = p->data; \ kmp_free(name, kl->mp, p); \ return 0; \ + } \ + SCOPE int kl_size_##name(kl_##name##_t *kl) { \ + if (kl == 0 || kl->head->next == 0) return 0; \ + return kl->size; \ } #define KLIST_INIT(name, kltype_t, kmpfree_t) \ @@ -131,5 +134,6 @@ #define kl_destroy(name, kl) kl_destroy_##name(kl) #define kl_pushp(name, kl) kl_pushp_##name(kl) #define kl_shift(name, kl, d) kl_shift_##name(kl, d) +#define kl_size(name, kl) kl_size_##name(kl) #endif diff --git a/test/klist_test.c b/test/klist_test.c index cd13813d..c3a43a58 100644 --- a/test/klist_test.c +++ b/test/klist_test.c @@ -7,11 +7,20 @@ KLIST_INIT(32, int, __int_free) int main() { klist_t(32) *kl; - kliter_t(32) *p; kl = kl_init(32); + printf("Initial size of list is %d\n", kl_size(32, kl)); *kl_pushp(32, kl) = 1; + *kl_pushp(32, kl) = 2; + *kl_pushp(32, kl) = 3; + *kl_pushp(32, kl) = 4; + *kl_pushp(32, kl) = 5; + *kl_pushp(32, kl) = 6; + *kl_pushp(32, kl) = 7; *kl_pushp(32, kl) = 10; + printf("Size of list after several pushp is %d\n", kl_size(32, kl)); kl_shift(32, kl, 0); + printf("Size of list after shift is %d\n", kl_size(32, kl)); + kliter_t(32) *p; for (p = kl_begin(kl); p != kl_end(kl); p = kl_next(p)) printf("%d\n", kl_val(p)); kl_destroy(32, kl);