From caa60ca993d1659b38cbcd441ad9e31b68181bf9 Mon Sep 17 00:00:00 2001 From: leiyu3 Date: Thu, 22 Sep 2022 12:33:41 -0400 Subject: fixes --- liblinked_list.c | 56 +++++++++++++++++++++++++++++++++++------------------- linked_list.h | 24 +++++++++++------------ linked_list_test.c | 30 +++++++++++------------------ 3 files changed, 59 insertions(+), 51 deletions(-) diff --git a/liblinked_list.c b/liblinked_list.c index 83689a5..7c0706b 100644 --- a/liblinked_list.c +++ b/liblinked_list.c @@ -2,11 +2,10 @@ #include #include "linked_list.h" -int count_ll(node* ptr){ +int count_ll(struct node* ptr){ /* Count the number of nodes in a linked list. */ - // TODO:: What if there is 0 item in the linked list?? int count = 0; while (ptr != NULL){ count++; @@ -15,31 +14,42 @@ int count_ll(node* ptr){ return count; } -void prepend_ll(node **ptr, int value){ +struct node* init_ll(int value){ + // can make this create node + // and then let other function call this + // since this is a repeated function in prepend append + struct node* ptr = malloc(sizeof(struct node)); + ptr->value = value; + ptr->next = NULL; + + return ptr; +} + +void prepend_ll(struct node **ptr, int value){ /* Add a node to the front of the linked list + Push */ - node* new = malloc(sizeof(node)); + struct node* new = malloc(sizeof(struct node)); new->value = value; new->next = *ptr; *ptr = new; - } -void append_ll(node* ptr, int value){ +void append_ll(struct node* ptr, int value){ /* Add a node to the end of the linked list */ - node* new = malloc(sizeof(node)); - new->value = value; - new->next = NULL; + struct node* new = malloc(sizeof(struct node)); while (ptr->next != NULL){ ptr = ptr->next; } + new->value = value; + new->next = NULL; ptr->next = new; } -void print_ll(node* ptr){ +void print_ll(struct node* ptr){ /* Print out the linked list */ @@ -54,11 +64,17 @@ void print_ll(node* ptr){ printf("\n"); } -void free_ll(node* ptr){ +void free_ll(struct node* ptr){ /* Safely free all nodes inside linked list. */ - node* next; + if (ptr == NULL){ + // if head is NULL then the list must be empty + // no memory to free + return; + } + + struct node* next; while (ptr->next != NULL){ next = ptr->next; free(ptr); @@ -66,7 +82,7 @@ void free_ll(node* ptr){ } } -int update_node(node* ptr, int index, int value){ +int update_node(struct node* ptr, int index, int value){ /* Update value of a node inside linked list */ @@ -87,13 +103,13 @@ int update_node(node* ptr, int index, int value){ return 0; } -int delete_node(node **ptr, int index){ +int delete_node(struct node **ptr, int index){ /* Delete a node given the index */ // TODO:: What if the node doesn't exist? - node* prev; + struct node* prev; if (index == 0){ prev = *ptr; *ptr = (*ptr)->next; @@ -101,7 +117,7 @@ int delete_node(node **ptr, int index){ return 0; } - node* root = *ptr; + struct node* root = *ptr; for (int i = 0; i <= index; i++){ if (i == index){ prev->next = root->next; @@ -116,7 +132,7 @@ int delete_node(node **ptr, int index){ exit(-1); } -int get_value(node *ptr, int index){ +int get_value(struct node *ptr, int index){ /* Get the value of a specific node given the index */ @@ -129,7 +145,7 @@ int get_value(node *ptr, int index){ } -node* search_node(node *ptr, int value){ +struct node* search_node(struct node *ptr, int value){ /* Search for a node given the value and returns it */ @@ -147,8 +163,8 @@ node* search_node(node *ptr, int value){ return ptr; } -void reverse_ll(node** ptr){ - node *prev, *cur, *next; +void reverse_ll(struct node** ptr){ + struct node *prev, *cur, *next; int n = count_ll(*ptr); if (n == 0){ diff --git a/linked_list.h b/linked_list.h index 1b2c3ca..40df9d7 100644 --- a/linked_list.h +++ b/linked_list.h @@ -1,22 +1,22 @@ #ifndef __LINKED_LIST_H__ #define __LINKED_LIST_H__ -typedef struct node node; struct node{ int value; - node* next; + struct node* next; }; -int count_ll(node* ptr); -void prepend_ll(node **ptr, int value); -void append_ll(node* ptr, int value); -void print_ll(node* ptr); -void free_ll(node* ptr); -int update_node(node* ptr, int index, int value); -int delete_node(node **ptr, int index); -int get_value(node *ptr, int index); -node* search_node(node *ptr, int value); -void reverse_ll(node **ptr); +int count_ll(struct node* ptr); +struct node* init_ll(int value); +void prepend_ll(struct node **ptr, int value); +void append_ll(struct node* ptr, int value); +void print_ll(struct node* ptr); +void free_ll(struct node* ptr); +int update_node(struct node* ptr, int index, int value); +int delete_node(struct node **ptr, int index); +int get_value(struct node *ptr, int index); +struct node* search_node(struct node *ptr, int value); +void reverse_ll(struct node **ptr); #endif diff --git a/linked_list_test.c b/linked_list_test.c index a71a1ff..7fab681 100644 --- a/linked_list_test.c +++ b/linked_list_test.c @@ -3,27 +3,19 @@ #include "linked_list.h" int main(){ - - node* ll = malloc(sizeof(node)); - ll->value = 1; - ll->next = NULL; - - + // should I make it so that when you make a node you have to first initialize it with a value? + // this means that we essentially can not have an empty list. + + // NULL will count as an empty list + struct node* ll = init_ll(1); + + /* prepend_ll(&ll, 1); */ + printf("%d\n",count_ll(ll)); print_ll(ll); - reverse_ll(&ll); - print_ll(ll); - /* delete_node(&ll, 0); */ - - /* print_ll(ll); */ - /* printf("The length of ll is %d\n", count_ll(ll)); */ - - /* delete_node(&ll, 0); */ - /* print_ll(ll); */ - /* printf("The length of ll is %d\n", count_ll(ll)); */ - /* printf("%d\n", search_node(ll, 10)->value); */ + /* delete_node(&ll,0); */ - /* printf("%d\n", get_value(ll, 3)); */ + /* printf("%d\n",count_ll(ll)); */ - /* free_ll(ll); */ + free_ll(ll); return 0; } -- cgit v1.2.3