From a95ee7387b86286af574d5c33305c967ff752069 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 30 Jan 2020 08:28:13 +0100 Subject: [PATCH] Added search function --- Demonstration/book.c | 6 +++++ Demonstration/book.h | 2 ++ Demonstration/main.c | 45 +++++++++++++++++++++++++++--------- Implementation/linked_list.c | 7 ++++++ Implementation/linked_list.h | 2 ++ 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/Demonstration/book.c b/Demonstration/book.c index 02d215c..29f691a 100644 --- a/Demonstration/book.c +++ b/Demonstration/book.c @@ -32,4 +32,10 @@ void book_print(const void* data) { const Book* book = (Book*)data; printf("Book[title=%s,author=%s,year=%d]\n", book->title, book->author, book->year); +} + +int title_eq(const void* title, const void* data) +{ + Book* book = (Book*)data; + return !strcmp(book->title, (char*) title); } \ No newline at end of file diff --git a/Demonstration/book.h b/Demonstration/book.h index 73b0339..1ca3a35 100644 --- a/Demonstration/book.h +++ b/Demonstration/book.h @@ -9,3 +9,5 @@ typedef struct { Book* book_create(const char* title, const char* author, int year); void book_print(const void* data); + +int title_eq(const void* title, const void* data); \ No newline at end of file diff --git a/Demonstration/main.c b/Demonstration/main.c index 988a031..1a7eb00 100644 --- a/Demonstration/main.c +++ b/Demonstration/main.c @@ -136,25 +136,48 @@ void show_print_pointers(Node** list) list_print_pointers(*list); } +void show_search(Node** list) +{ + char title[80]; + Node* book; + + printf("Enter the title to search for: "); + gets_s(title, 80); + book = list_search(*list, title, &title_eq); + if (book) + book_print(book->data); + else + puts("No list item matched the given title."); +} + +#define item(NAME, KEY, FUNC) {NAME, KEY, (void*)&FUNC, &list} +#define page(NAME, ARR) {ARR, sizeof(ARR) / sizeof(struct MenuItem), NAME, true, true, &SOLID} + void main(void) { Node* list = NULL; - struct MenuItem items[] = { - {"Push item", '1', (void*)&show_push, &list}, - {"Append item", '2', (void*)&show_append, &list}, - {"Insert item", '3', (void*)&show_insert, &list}, - {"Print item", '4', (void*)&show_print, &list}, - {"Look at item by index", '5', (void*)&show_get, &list}, - {"Remove item by index", '6', (void*)&show_remove, &list}, - {"READ", 'r', (void*)&show_read, &list}, - {"RIDE", 'w', (void*)&show_write, &list}, - {"Print pointers", 'p', (void*)show_print_pointers, &list}, + struct MenuItem items_modification[] = { + item("Push Item", '1', show_push), + item("Append item", '2', show_append), + item("Insert item", '3', show_insert), + item("Remove item by index", '4', show_remove), + {"BLANK", ' ', NULL, NULL}, + {"Quit", 'q', (void*)&exit, NULL} + }; + struct MenuItem items_util[] = { + item("Print list", '1', show_print), + item("Print list element by index", '2', show_get), + item("Search for title", '3', show_search), + item("Read", 'r', show_read), + item("Write", 'w', show_write), + item("Print pointers", 'p', show_print_pointers), {"BLANK", ' ', NULL, NULL}, {"Quit", 'q', (void*)&exit, NULL} }; struct MenuPage pages[] = { - {items, sizeof(items) / sizeof(struct MenuItem), "Linked List Demo", true, true, &SOLID} + page("Linked List Modification", items_modification), + page("Linked List Utility", items_util), }; show_menu(pages, sizeof(pages) / sizeof(struct MenuPage), true); } \ No newline at end of file diff --git a/Implementation/linked_list.c b/Implementation/linked_list.c index 0a588bb..866d0c8 100644 --- a/Implementation/linked_list.c +++ b/Implementation/linked_list.c @@ -86,6 +86,13 @@ Node* list_get(Node* head, unsigned index) return head; } +Node* list_search(Node* head, void* elem, int (*eq)(void*, void*)) +{ + while (head && !eq(elem, head->data)) + head = head->next; + return head; +} + void list_remove(Node* node) { if (node->prev) diff --git a/Implementation/linked_list.h b/Implementation/linked_list.h index 1f1d902..5786711 100644 --- a/Implementation/linked_list.h +++ b/Implementation/linked_list.h @@ -17,4 +17,6 @@ void list_insert(Node* prev_node, void* data); Node* list_get(Node* head, unsigned index); +Node* list_search(Node* head, void* elem, int (*eq)(void*, void*)); + void list_remove(Node* node); \ No newline at end of file