131 lines
2.8 KiB
C
131 lines
2.8 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "../Implementation/linked_list.h"
|
|
#include "../../MenuLib/menu.h"
|
|
|
|
typedef struct {
|
|
char* title;
|
|
char* author;
|
|
int year;
|
|
} Book;
|
|
|
|
Book* book_create(const char* title, const char* author, int year)
|
|
{
|
|
// initialize book
|
|
Book* book = calloc(1, sizeof(Book));
|
|
if (!book)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
// initialize title and author
|
|
book->title = calloc(strlen(title), sizeof(char));
|
|
book->author = calloc(strlen(author), sizeof(char));
|
|
if (!book->title || !book->author)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
strcpy(book->title, title);
|
|
strcpy(book->author, author);
|
|
book->year = year;
|
|
|
|
return book;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void show_push(Node** list)
|
|
{
|
|
char title[101], author[101];
|
|
int year;
|
|
|
|
printf("Enter title of Book: ");
|
|
scanf_s(" %[^\n]s", title, 101);
|
|
printf("Enter author of Book: ");
|
|
scanf_s(" %[^\n]s", author, 101);
|
|
printf("Enter year of Release: ");
|
|
scanf_s(" %d", &year);
|
|
|
|
list_push(list, book_create(title, author, year));
|
|
}
|
|
|
|
void show_append(Node** list)
|
|
{
|
|
char title[101], author[101];
|
|
int year;
|
|
|
|
printf("Enter title of Book: ");
|
|
scanf_s(" %[^\n]s", title, 101);
|
|
printf("Enter author of Book: ");
|
|
scanf_s(" %[^\n]s", author, 101);
|
|
printf("Enter year of Release: ");
|
|
scanf_s(" %d", &year);
|
|
|
|
list_append(list, book_create(title, author, year));
|
|
}
|
|
|
|
void show_insert(Node** list)
|
|
{
|
|
char title[101], author[101];
|
|
int year, index;
|
|
|
|
printf("Enter title of Book: ");
|
|
scanf_s(" %[^\n]s", title, 101);
|
|
printf("Enter author of Book: ");
|
|
scanf_s(" %[^\n]s", author, 101);
|
|
printf("Enter year of Release: ");
|
|
scanf_s(" %d", &year);
|
|
printf("Enter place after which book is inserted: ");
|
|
scanf_s(" %d", &index);
|
|
|
|
list_insert(list_get(list, index), book_create(title, author, year));
|
|
}
|
|
|
|
void show_print(Node** list)
|
|
{
|
|
list_print(*list, &book_print);
|
|
}
|
|
|
|
void show_get(Node** list)
|
|
{
|
|
int index;
|
|
printf("Enter place: ");
|
|
scanf_s(" %d", &index);
|
|
list_print(list_get(list,index), &book_print);
|
|
}
|
|
|
|
void show_remove(Node** list)
|
|
{
|
|
int index;
|
|
printf("Enter place: ");
|
|
scanf_s(" %d", &index);
|
|
list_remove(list_get(list, index));
|
|
}
|
|
|
|
|
|
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},
|
|
{"BLANK", NULL, NULL, NULL},
|
|
{"Quit", 'q', (void*)&exit, NULL}
|
|
};
|
|
struct MenuPage pages[] = {
|
|
{items, sizeof(items) / sizeof(struct MenuItem), "Linked List Demo", true, true, &SOLID}
|
|
};
|
|
show_menu(pages, sizeof(pages) / sizeof(struct MenuPage), true);
|
|
} |