From ae70615e4e730a83e710dd2418b550141dcfe6fc Mon Sep 17 00:00:00 2001 From: rhinemann Date: Mon, 16 Sep 2024 19:46:58 +0300 Subject: [PATCH] Linked list task finished --- ll.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/ll.c b/ll.c index b453418..36f4fa9 100644 --- a/ll.c +++ b/ll.c @@ -1,6 +1,66 @@ #include +#include + +typedef struct book { + char *name; + double price; + int page_quantity; + char *language; + int weight_grams; + int year_published; + struct book *next; +} HPBook; + +void new_book(HPBook *book, char *name, double price, int page_quantity, char *language, int weight_grams, int year_published, HPBook *next) { + book->name = name; + book->price = price; + book->page_quantity = page_quantity; + book->language = language; + book->weight_grams = weight_grams; + book->year_published = year_published; + book->next = next; +} + +void print_book(HPBook *book) { + printf("Name: %s\n", book->name); + printf(" Price: %.2lf\n", book->price); + printf(" Pages: %d\n", book->page_quantity); + printf(" Language: %s\n", book->language); + printf(" Year of publication: %d\n", book->year_published); + for (int i = 0; i < 50; i++) { + printf("─"); + } + printf("\n"); +} + +void print_linked_list(HPBook *book) { + while (book != NULL) { + print_book(book); + book = book->next; + } +} int main() { - printf("Hello, World!\n"); - return 0; + HPBook *head = NULL, *philosopher = NULL, *chamber = NULL, *prisoner = NULL, *goblet = NULL, *order = NULL, *prince = NULL, *hallows = NULL; + + philosopher = (HPBook *) malloc(sizeof(HPBook)); + chamber = (HPBook *) malloc(sizeof(HPBook)); + prisoner = (HPBook *) malloc(sizeof(HPBook)); + goblet = (HPBook *) malloc(sizeof(HPBook)); + order = (HPBook *) malloc(sizeof(HPBook)); + prince = (HPBook *) malloc(sizeof(HPBook)); + hallows = (HPBook *) malloc(sizeof(HPBook)); + + new_book(philosopher, "Harry Potter and the Philosopher's Stone", 990, 248, "Ua", 100, 2016, chamber); + new_book(chamber, "Harry Potter and the Chamber of Secrets", 990, 272, "Ua", 100, 2016, prisoner); + new_book(prisoner, "Harry Potter and the Prisoner of Azkaban", 1100, 336, "Ua", 100, 2017, goblet); + new_book(goblet, "Harry Potter and the Goblet of Fire", 1200, 464, "Ua", 100, 2019, order); + new_book(order, "Harry Potter and the Order of the Phoenix", 1400, 576, "Ua", 100, 2022, prince); + new_book(prince, "Harry Potter and the Half-Blood Prince", 420, 576, "Ua", 100, 2005, hallows); + new_book(hallows, "Harry Potter and the Deathly Hallows", 420, 640, "Ua", 100, 2007, NULL); + + head = philosopher; + print_linked_list(head); + + return 0; }