Added search function
This commit is contained in:
parent
560ac1e59f
commit
a95ee7387b
@ -33,3 +33,9 @@ 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);
|
||||
}
|
@ -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);
|
@ -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);
|
||||
}
|
@ -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)
|
||||
|
@ -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);
|
Reference in New Issue
Block a user