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 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 20 deletions(-) (limited to 'liblinked_list.c') 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){ -- cgit v1.2.3