Archived
1
This repository has been archived on 2020-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
linked-list/Demonstration/main.c

183 lines
3.8 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../Implementation/linked_list.h"
#include "../../MenuLib/menu.h"
#include "book.h"
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 show_read(Node** list)
{
char path[101], line[256], * title, * author, * year;
FILE* CSV;
printf("Enter the path of the CSV file to open: ");
gets_s(path, 101);
if (!(CSV = fopen(path, "r")))
{
puts("Failed to read file!\n");
return;
}
while (fgets(line, 256, CSV))
{
printf("Found line %s", line);
title = strtok(line, ";");
author = strtok(NULL, ";");
year = strtok(NULL, ";");
printf("%s, %s, %s\n", title, author, year);
list_push(list, book_create(title, author, atoi(year)));
}
fclose(CSV);
}
void show_write(Node** list)
{
char path[101];
FILE* CSV;
Node* node = *list;
Book* book;
printf("Enter the path of the file to write: ");
gets_s(path, 101);
if (!(CSV = fopen(path, "w")))
{
puts("Failed to write file!\n");
return;
}
while (node)
{
book = (Book*)node->data;
fprintf(CSV, "%s;%s;%d\n", book->title, book->author, book->year);
node = node->next;
}
fclose(CSV);
}
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_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[] = {
page("Linked List Modification", items_modification),
page("Linked List Utility", items_util),
};
show_menu(pages, sizeof(pages) / sizeof(struct MenuPage), true);
}