diff options
Diffstat (limited to 'liblinked_list.c')
| -rw-r--r-- | liblinked_list.c | 56 |
1 files changed, 36 insertions, 20 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 @@ | |||
| 2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 3 | #include "linked_list.h" | 3 | #include "linked_list.h" |
| 4 | 4 | ||
| 5 | int count_ll(node* ptr){ | 5 | int count_ll(struct node* ptr){ |
| 6 | /* | 6 | /* |
| 7 | Count the number of nodes in a linked list. | 7 | Count the number of nodes in a linked list. |
| 8 | */ | 8 | */ |
| 9 | // TODO:: What if there is 0 item in the linked list?? | ||
| 10 | int count = 0; | 9 | int count = 0; |
| 11 | while (ptr != NULL){ | 10 | while (ptr != NULL){ |
| 12 | count++; | 11 | count++; |
| @@ -15,31 +14,42 @@ int count_ll(node* ptr){ | |||
| 15 | return count; | 14 | return count; |
| 16 | } | 15 | } |
| 17 | 16 | ||
| 18 | void prepend_ll(node **ptr, int value){ | 17 | struct node* init_ll(int value){ |
| 18 | // can make this create node | ||
| 19 | // and then let other function call this | ||
| 20 | // since this is a repeated function in prepend append | ||
| 21 | struct node* ptr = malloc(sizeof(struct node)); | ||
| 22 | ptr->value = value; | ||
| 23 | ptr->next = NULL; | ||
| 24 | |||
| 25 | return ptr; | ||
| 26 | } | ||
| 27 | |||
| 28 | void prepend_ll(struct node **ptr, int value){ | ||
| 19 | /* | 29 | /* |
| 20 | Add a node to the front of the linked list | 30 | Add a node to the front of the linked list |
| 31 | Push | ||
| 21 | */ | 32 | */ |
| 22 | node* new = malloc(sizeof(node)); | 33 | struct node* new = malloc(sizeof(struct node)); |
| 23 | new->value = value; | 34 | new->value = value; |
| 24 | new->next = *ptr; | 35 | new->next = *ptr; |
| 25 | *ptr = new; | 36 | *ptr = new; |
| 26 | |||
| 27 | } | 37 | } |
| 28 | 38 | ||
| 29 | void append_ll(node* ptr, int value){ | 39 | void append_ll(struct node* ptr, int value){ |
| 30 | /* | 40 | /* |
| 31 | Add a node to the end of the linked list | 41 | Add a node to the end of the linked list |
| 32 | */ | 42 | */ |
| 33 | node* new = malloc(sizeof(node)); | 43 | struct node* new = malloc(sizeof(struct node)); |
| 34 | new->value = value; | ||
| 35 | new->next = NULL; | ||
| 36 | while (ptr->next != NULL){ | 44 | while (ptr->next != NULL){ |
| 37 | ptr = ptr->next; | 45 | ptr = ptr->next; |
| 38 | } | 46 | } |
| 47 | new->value = value; | ||
| 48 | new->next = NULL; | ||
| 39 | ptr->next = new; | 49 | ptr->next = new; |
| 40 | } | 50 | } |
| 41 | 51 | ||
| 42 | void print_ll(node* ptr){ | 52 | void print_ll(struct node* ptr){ |
| 43 | /* | 53 | /* |
| 44 | Print out the linked list | 54 | Print out the linked list |
| 45 | */ | 55 | */ |
| @@ -54,11 +64,17 @@ void print_ll(node* ptr){ | |||
| 54 | printf("\n"); | 64 | printf("\n"); |
| 55 | } | 65 | } |
| 56 | 66 | ||
| 57 | void free_ll(node* ptr){ | 67 | void free_ll(struct node* ptr){ |
| 58 | /* | 68 | /* |
| 59 | Safely free all nodes inside linked list. | 69 | Safely free all nodes inside linked list. |
| 60 | */ | 70 | */ |
| 61 | node* next; | 71 | if (ptr == NULL){ |
| 72 | // if head is NULL then the list must be empty | ||
| 73 | // no memory to free | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | struct node* next; | ||
| 62 | while (ptr->next != NULL){ | 78 | while (ptr->next != NULL){ |
| 63 | next = ptr->next; | 79 | next = ptr->next; |
| 64 | free(ptr); | 80 | free(ptr); |
| @@ -66,7 +82,7 @@ void free_ll(node* ptr){ | |||
| 66 | } | 82 | } |
| 67 | } | 83 | } |
| 68 | 84 | ||
| 69 | int update_node(node* ptr, int index, int value){ | 85 | int update_node(struct node* ptr, int index, int value){ |
| 70 | /* | 86 | /* |
| 71 | Update value of a node inside linked list | 87 | Update value of a node inside linked list |
| 72 | */ | 88 | */ |
| @@ -87,13 +103,13 @@ int update_node(node* ptr, int index, int value){ | |||
| 87 | return 0; | 103 | return 0; |
| 88 | } | 104 | } |
| 89 | 105 | ||
| 90 | int delete_node(node **ptr, int index){ | 106 | int delete_node(struct node **ptr, int index){ |
| 91 | /* | 107 | /* |
| 92 | Delete a node given the index | 108 | Delete a node given the index |
| 93 | */ | 109 | */ |
| 94 | 110 | ||
| 95 | // TODO:: What if the node doesn't exist? | 111 | // TODO:: What if the node doesn't exist? |
| 96 | node* prev; | 112 | struct node* prev; |
| 97 | if (index == 0){ | 113 | if (index == 0){ |
| 98 | prev = *ptr; | 114 | prev = *ptr; |
| 99 | *ptr = (*ptr)->next; | 115 | *ptr = (*ptr)->next; |
| @@ -101,7 +117,7 @@ int delete_node(node **ptr, int index){ | |||
| 101 | return 0; | 117 | return 0; |
| 102 | } | 118 | } |
| 103 | 119 | ||
| 104 | node* root = *ptr; | 120 | struct node* root = *ptr; |
| 105 | for (int i = 0; i <= index; i++){ | 121 | for (int i = 0; i <= index; i++){ |
| 106 | if (i == index){ | 122 | if (i == index){ |
| 107 | prev->next = root->next; | 123 | prev->next = root->next; |
| @@ -116,7 +132,7 @@ int delete_node(node **ptr, int index){ | |||
| 116 | exit(-1); | 132 | exit(-1); |
| 117 | } | 133 | } |
| 118 | 134 | ||
| 119 | int get_value(node *ptr, int index){ | 135 | int get_value(struct node *ptr, int index){ |
| 120 | /* | 136 | /* |
| 121 | Get the value of a specific node given the index | 137 | Get the value of a specific node given the index |
| 122 | */ | 138 | */ |
| @@ -129,7 +145,7 @@ int get_value(node *ptr, int index){ | |||
| 129 | 145 | ||
| 130 | } | 146 | } |
| 131 | 147 | ||
| 132 | node* search_node(node *ptr, int value){ | 148 | struct node* search_node(struct node *ptr, int value){ |
| 133 | /* | 149 | /* |
| 134 | Search for a node given the value and returns it | 150 | Search for a node given the value and returns it |
| 135 | */ | 151 | */ |
| @@ -147,8 +163,8 @@ node* search_node(node *ptr, int value){ | |||
| 147 | return ptr; | 163 | return ptr; |
| 148 | } | 164 | } |
| 149 | 165 | ||
| 150 | void reverse_ll(node** ptr){ | 166 | void reverse_ll(struct node** ptr){ |
| 151 | node *prev, *cur, *next; | 167 | struct node *prev, *cur, *next; |
| 152 | int n = count_ll(*ptr); | 168 | int n = count_ll(*ptr); |
| 153 | 169 | ||
| 154 | if (n == 0){ | 170 | if (n == 0){ |
