#include #include #include #include #include struct Block { char text[64]; struct TailQ blocks; }; struct File { char name[128]; struct TailQ head; }; void add_block(struct File *f, const char *txt) { struct Block *b = malloc(sizeof *b); strcpy(b->text,txt); uc_tailq_insert_tail(&f->head, &b->blocks); } void print_file(struct File *f) { struct Block *entry; printf("File: %s\n", f->name); UC_TAILQ_FOREACH_CONTAINER(entry, blocks, &f->head) { printf("Block: %s\n", entry->text); } } void print_file_reverse(struct File *f) { struct Block *entry; printf("File: %s\n", f->name); UC_TAILQ_FOREACH_REVERSE_CONTAINER(entry, blocks, &f->head) { printf("Block: %s\n", entry->text); } } void delete_block(struct File *f, const char *txt) { struct Block *entry, *next; UC_TAILQ_FOREACH_CONTAINER_SAFE(entry, next, blocks, &f->head) { if (strcmp(txt, entry->text) == 0) { uc_tailq_remove(&entry->blocks); free(entry); } } } void delete_all_blocks(struct File *f) { struct Block *entry, *next; UC_TAILQ_FOREACH_CONTAINER_SAFE(entry, next, blocks, &f->head) { free(entry); } uc_tailq_init(&f->head); } int main(void) { struct File f; uc_tailq_init(&f.head); printf("Is Empty: %d\n", uc_tailq_empty(&f.head)); strcpy(f.name,"Test.f"); add_block(&f,"one"); add_block(&f,"two"); add_block(&f,"three"); print_file(&f); printf("Is Empty: %d\n", uc_tailq_empty(&f.head)); puts("\nDeleting three"); delete_block(&f, "three"); print_file(&f); puts("\nAdding 2 blocks"); add_block(&f,"three"); add_block(&f,"three"); print_file(&f); puts("\nReverse order:"); print_file_reverse(&f); puts("\nDeleting three"); delete_block(&f, "three"); print_file(&f); delete_all_blocks(&f); printf("Is Empty: %d\n", uc_tailq_empty(&f.head)); return 0; }