Added search function
This commit is contained in:
parent
560ac1e59f
commit
a95ee7387b
@ -32,4 +32,10 @@ void book_print(const void* data)
|
|||||||
{
|
{
|
||||||
const Book* book = (Book*)data;
|
const Book* book = (Book*)data;
|
||||||
printf("Book[title=%s,author=%s,year=%d]\n", book->title, book->author, book->year);
|
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);
|
Book* book_create(const char* title, const char* author, int year);
|
||||||
|
|
||||||
void book_print(const void* data);
|
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);
|
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)
|
void main(void)
|
||||||
{
|
{
|
||||||
Node* list = NULL;
|
Node* list = NULL;
|
||||||
|
|
||||||
struct MenuItem items[] = {
|
struct MenuItem items_modification[] = {
|
||||||
{"Push item", '1', (void*)&show_push, &list},
|
item("Push Item", '1', show_push),
|
||||||
{"Append item", '2', (void*)&show_append, &list},
|
item("Append item", '2', show_append),
|
||||||
{"Insert item", '3', (void*)&show_insert, &list},
|
item("Insert item", '3', show_insert),
|
||||||
{"Print item", '4', (void*)&show_print, &list},
|
item("Remove item by index", '4', show_remove),
|
||||||
{"Look at item by index", '5', (void*)&show_get, &list},
|
{"BLANK", ' ', NULL, NULL},
|
||||||
{"Remove item by index", '6', (void*)&show_remove, &list},
|
{"Quit", 'q', (void*)&exit, NULL}
|
||||||
{"READ", 'r', (void*)&show_read, &list},
|
};
|
||||||
{"RIDE", 'w', (void*)&show_write, &list},
|
struct MenuItem items_util[] = {
|
||||||
{"Print pointers", 'p', (void*)show_print_pointers, &list},
|
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},
|
{"BLANK", ' ', NULL, NULL},
|
||||||
{"Quit", 'q', (void*)&exit, NULL}
|
{"Quit", 'q', (void*)&exit, NULL}
|
||||||
};
|
};
|
||||||
struct MenuPage pages[] = {
|
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);
|
show_menu(pages, sizeof(pages) / sizeof(struct MenuPage), true);
|
||||||
}
|
}
|
@ -86,6 +86,13 @@ Node* list_get(Node* head, unsigned index)
|
|||||||
return head;
|
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)
|
void list_remove(Node* node)
|
||||||
{
|
{
|
||||||
if (node->prev)
|
if (node->prev)
|
||||||
|
@ -17,4 +17,6 @@ void list_insert(Node* prev_node, void* data);
|
|||||||
|
|
||||||
Node* list_get(Node* head, unsigned index);
|
Node* list_get(Node* head, unsigned index);
|
||||||
|
|
||||||
|
Node* list_search(Node* head, void* elem, int (*eq)(void*, void*));
|
||||||
|
|
||||||
void list_remove(Node* node);
|
void list_remove(Node* node);
|
Reference in New Issue
Block a user