// A linked list book shelf. // Books can be inserted and removed // Books must be inserted in the right order // Order is by Genre first, then title. // 3 functions, insert book, remove book, print bookshelf #include #include #include #define MAX_TITLE_LEN 100 enum category { HORROR, CRIME, ROMANCE, COMEDY, FANTASY }; struct book { char title[MAX_TITLE_LEN]; int pages; enum category genre; }; struct node { struct book data; struct node *next; }; struct book create_book(char title[MAX_TITLE_LEN], enum category genre, int pages) { struct book new_book; new_book.pages = pages; strcpy(new_book.title, title); new_book.genre = genre; return new_book; } void print_book(struct book book_to_print) { fputs(book_to_print.title, stdout); if (book_to_print.genre == HORROR) { printf(" (Horror)\n"); } else if (book_to_print.genre == FANTASY) { printf(" (Fantasy)\n"); } else if (book_to_print.genre == COMEDY) { printf(" (Comedy)\n"); } else if (book_to_print.genre == CRIME) { printf(" (Crime)\n"); } } void print_bookshelf(struct node *shelf_to_print) { printf("Jake's amazing bookshelf\n\n"); struct node *current = shelf_to_print; while (current != NULL) { print_book(current->data); current = current->next->next; } } struct node *create_node(struct book data, struct node *next) { struct node *new_node = malloc(sizeof(struct node)); new_node->data = data; new_node->next = next; return new_node; } struct node *insert_book(struct book to_insert, struct node *shelf) { // empty bookshelf if (shelf == NULL) { return create_node(to_insert, NULL); } // now we iterate through struct node *current = shelf; printf("\n\nPrinting books until we find a genre match\n"); while (current->data.genre != to_insert.genre) { print_book(current->data); current = current->next; } while (current->next->data.genre == to_insert.genre && strcmp(to_insert.title, current->next->data.title) > 0) { print_book(current->data); current = current->next; } current->next = create_node(to_insert, current->next); return shelf; } struct node *create_starter_shelf() { char title[MAX_TITLE_LEN]; strcpy(title, "Plean Code"); struct book new_book = create_book(title, HORROR, 200); char title_2[MAX_TITLE_LEN]; strcpy(title_2, "Crime and Punishment"); struct book new_book_2 = create_book(title_2, CRIME, 200); char title_3[MAX_TITLE_LEN]; strcpy(title_3, "Peter Pan"); struct book new_book_3 = create_book(title_3, FANTASY, 200); char title_4[MAX_TITLE_LEN]; strcpy(title_4, "The Lion the Witch and the Wardrobe"); struct book new_book_4 = create_book(title_4, FANTASY, 200); char title_5[MAX_TITLE_LEN]; strcpy(title_5, "COMP1511 Handbook"); struct book new_book_5 = create_book(title_5, COMEDY, 200); struct node *shelf = NULL; shelf = create_node(new_book_5, shelf); shelf = create_node(new_book_4, shelf); shelf = create_node(new_book_3, shelf); shelf = create_node(new_book_2, shelf); shelf = create_node(new_book, shelf); return shelf; } int main(void) { struct node *shelf = create_starter_shelf(); print_bookshelf(shelf); // insert a new fantasy book char title[MAX_TITLE_LEN]; strcpy(title, "The Priory of the Orange Tree"); struct book new_book = create_book(title, FANTASY, 200); shelf = insert_book(new_book, shelf); printf("\n\n printing shelf\n"); print_bookshelf(shelf); // don't forget to free all books return 0; }