From 560ac1e59f5a19fc7fb699faf85d2cdcd012996d Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 30 Jan 2020 07:48:20 +0100 Subject: [PATCH] Added list pointer printing function --- Demonstration/main.c | 22 ++++++++++++++-------- Implementation/linked_list.c | 11 +++++++++++ Implementation/linked_list.h | 2 ++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Demonstration/main.c b/Demonstration/main.c index a9aaf94..988a031 100644 --- a/Demonstration/main.c +++ b/Demonstration/main.c @@ -46,9 +46,9 @@ void show_insert(Node** list) printf("Enter author of Book: "); scanf_s(" %[^\n]s", author, 101); printf("Enter year of Release: "); - scanf_s(" %d", &year); + scanf_s("%d", &year); printf("Enter place after which book is inserted: "); - scanf_s(" %d", &index); + scanf_s("%d", &index); list_insert(list_get(*list, index), book_create(title, author, year)); } @@ -62,7 +62,7 @@ void show_get(Node** list) { int index; printf("Enter place: "); - scanf_s(" %d", &index); + scanf_s("%d", &index); list_print(list_get(*list, index), &book_print); } @@ -70,7 +70,7 @@ void show_remove(Node** list) { int index; printf("Enter place: "); - scanf_s(" %d", &index); + scanf_s("%d", &index); list_remove(list_get(*list, index)); } @@ -79,7 +79,7 @@ void show_read(Node** list) char path[101], line[256], * title, * author, * year; FILE* CSV; - printf("Enter the path to the file of haram: "); + printf("Enter the path of the CSV file to open: "); gets_s(path, 101); if (!(CSV = fopen(path, "r"))) @@ -107,17 +107,17 @@ void show_read(Node** list) void show_write(Node** list) { - char path[101], line[256]; + char path[101]; FILE* CSV; Node* node = *list; Book* book; - printf("Enter the path to the file_s: "); + printf("Enter the path of the file to write: "); gets_s(path, 101); if (!(CSV = fopen(path, "w"))) { - puts("Failed to read file!\n"); + puts("Failed to write file!\n"); return; } @@ -131,6 +131,11 @@ void show_write(Node** list) fclose(CSV); } +void show_print_pointers(Node** list) +{ + list_print_pointers(*list); +} + void main(void) { Node* list = NULL; @@ -144,6 +149,7 @@ void main(void) {"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}, {"BLANK", ' ', NULL, NULL}, {"Quit", 'q', (void*)&exit, NULL} }; diff --git a/Implementation/linked_list.c b/Implementation/linked_list.c index 0a8dec4..0a588bb 100644 --- a/Implementation/linked_list.c +++ b/Implementation/linked_list.c @@ -1,6 +1,7 @@ #include "linked_list.h" #include +#include static Node* create(void* data) { @@ -20,6 +21,16 @@ void list_print(Node* head, void (*print_func)(void*)) } } +void list_print_pointers(Node* head) +{ + printf("%-19s%-19s%-19s%-19s\n", "Prev", "Current", "Next", "Data"); + while (head) + { + printf("0x%p 0x%p 0x%p 0x%p\n", head->prev, head, head->next, head->data); + head = head->next; + } +} + void list_push(Node** head, void* data) { Node* node = create(data); diff --git a/Implementation/linked_list.h b/Implementation/linked_list.h index 7e9c577..1f1d902 100644 --- a/Implementation/linked_list.h +++ b/Implementation/linked_list.h @@ -7,6 +7,8 @@ typedef struct Node { void list_print(Node* head, void (*print_func)(void*)); +void list_print_pointers(Node* head); + void list_push(Node** head, void* data); void list_append(Node** head, void* data);