Archived
1

Added list pointer printing function

This commit is contained in:
Kai S. K. Engelbart 2020-01-30 07:48:20 +01:00
parent b22a0977aa
commit 560ac1e59f
3 changed files with 27 additions and 8 deletions

View File

@ -46,9 +46,9 @@ void show_insert(Node** list)
printf("Enter author of Book: "); printf("Enter author of Book: ");
scanf_s(" %[^\n]s", author, 101); scanf_s(" %[^\n]s", author, 101);
printf("Enter year of Release: "); printf("Enter year of Release: ");
scanf_s(" %d", &year); scanf_s("%d", &year);
printf("Enter place after which book is inserted: "); 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)); list_insert(list_get(*list, index), book_create(title, author, year));
} }
@ -62,7 +62,7 @@ void show_get(Node** list)
{ {
int index; int index;
printf("Enter place: "); printf("Enter place: ");
scanf_s(" %d", &index); scanf_s("%d", &index);
list_print(list_get(*list, index), &book_print); list_print(list_get(*list, index), &book_print);
} }
@ -70,7 +70,7 @@ void show_remove(Node** list)
{ {
int index; int index;
printf("Enter place: "); printf("Enter place: ");
scanf_s(" %d", &index); scanf_s("%d", &index);
list_remove(list_get(*list, index)); list_remove(list_get(*list, index));
} }
@ -79,7 +79,7 @@ void show_read(Node** list)
char path[101], line[256], * title, * author, * year; char path[101], line[256], * title, * author, * year;
FILE* CSV; 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); gets_s(path, 101);
if (!(CSV = fopen(path, "r"))) if (!(CSV = fopen(path, "r")))
@ -107,17 +107,17 @@ void show_read(Node** list)
void show_write(Node** list) void show_write(Node** list)
{ {
char path[101], line[256]; char path[101];
FILE* CSV; FILE* CSV;
Node* node = *list; Node* node = *list;
Book* book; Book* book;
printf("Enter the path to the file_s: "); printf("Enter the path of the file to write: ");
gets_s(path, 101); gets_s(path, 101);
if (!(CSV = fopen(path, "w"))) if (!(CSV = fopen(path, "w")))
{ {
puts("Failed to read file!\n"); puts("Failed to write file!\n");
return; return;
} }
@ -131,6 +131,11 @@ void show_write(Node** list)
fclose(CSV); fclose(CSV);
} }
void show_print_pointers(Node** list)
{
list_print_pointers(*list);
}
void main(void) void main(void)
{ {
Node* list = NULL; Node* list = NULL;
@ -144,6 +149,7 @@ void main(void)
{"Remove item by index", '6', (void*)&show_remove, &list}, {"Remove item by index", '6', (void*)&show_remove, &list},
{"READ", 'r', (void*)&show_read, &list}, {"READ", 'r', (void*)&show_read, &list},
{"RIDE", 'w', (void*)&show_write, &list}, {"RIDE", 'w', (void*)&show_write, &list},
{"Print pointers", 'p', (void*)show_print_pointers, &list},
{"BLANK", ' ', NULL, NULL}, {"BLANK", ' ', NULL, NULL},
{"Quit", 'q', (void*)&exit, NULL} {"Quit", 'q', (void*)&exit, NULL}
}; };

View File

@ -1,6 +1,7 @@
#include "linked_list.h" #include "linked_list.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
static Node* create(void* data) 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) void list_push(Node** head, void* data)
{ {
Node* node = create(data); Node* node = create(data);

View File

@ -7,6 +7,8 @@ typedef struct Node {
void list_print(Node* head, void (*print_func)(void*)); void list_print(Node* head, void (*print_func)(void*));
void list_print_pointers(Node* head);
void list_push(Node** head, void* data); void list_push(Node** head, void* data);
void list_append(Node** head, void* data); void list_append(Node** head, void* data);