diff options
| author | leiyu3 <s444814187@gmail.com> | 2022-09-13 18:11:37 -0400 |
|---|---|---|
| committer | leiyu3 <s444814187@gmail.com> | 2022-09-13 18:11:37 -0400 |
| commit | e5efa02c2746a4207251daf0173b4808dd087e89 (patch) | |
| tree | e26425c1d27e45ffaa10cfb91675100a0de53908 /liblinked_list.c | |
| download | linked_list_c-e5efa02c2746a4207251daf0173b4808dd087e89.tar.gz linked_list_c-e5efa02c2746a4207251daf0173b4808dd087e89.zip | |
init
Diffstat (limited to 'liblinked_list.c')
| -rw-r--r-- | liblinked_list.c | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/liblinked_list.c b/liblinked_list.c new file mode 100644 index 0000000..83689a5 --- /dev/null +++ b/liblinked_list.c | |||
| @@ -0,0 +1,179 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include "linked_list.h" | ||
| 4 | |||
| 5 | int count_ll(node* ptr){ | ||
| 6 | /* | ||
| 7 | Count the number of nodes in a linked list. | ||
| 8 | */ | ||
| 9 | // TODO:: What if there is 0 item in the linked list?? | ||
| 10 | int count = 0; | ||
| 11 | while (ptr != NULL){ | ||
| 12 | count++; | ||
| 13 | ptr = ptr->next; | ||
| 14 | } | ||
| 15 | return count; | ||
| 16 | } | ||
| 17 | |||
| 18 | void prepend_ll(node **ptr, int value){ | ||
| 19 | /* | ||
| 20 | Add a node to the front of the linked list | ||
| 21 | */ | ||
| 22 | node* new = malloc(sizeof(node)); | ||
| 23 | new->value = value; | ||
| 24 | new->next = *ptr; | ||
| 25 | *ptr = new; | ||
| 26 | |||
| 27 | } | ||
| 28 | |||
| 29 | void append_ll(node* ptr, int value){ | ||
| 30 | /* | ||
| 31 | Add a node to the end of the linked list | ||
| 32 | */ | ||
| 33 | node* new = malloc(sizeof(node)); | ||
| 34 | new->value = value; | ||
| 35 | new->next = NULL; | ||
| 36 | while (ptr->next != NULL){ | ||
| 37 | ptr = ptr->next; | ||
| 38 | } | ||
| 39 | ptr->next = new; | ||
| 40 | } | ||
| 41 | |||
| 42 | void print_ll(node* ptr){ | ||
| 43 | /* | ||
| 44 | Print out the linked list | ||
| 45 | */ | ||
| 46 | while (ptr != NULL){ | ||
| 47 | printf("%d", ptr->value); | ||
| 48 | ptr = ptr->next; | ||
| 49 | if (ptr == NULL){ | ||
| 50 | break; | ||
| 51 | } | ||
| 52 | printf(" - "); | ||
| 53 | } | ||
| 54 | printf("\n"); | ||
| 55 | } | ||
| 56 | |||
| 57 | void free_ll(node* ptr){ | ||
| 58 | /* | ||
| 59 | Safely free all nodes inside linked list. | ||
| 60 | */ | ||
| 61 | node* next; | ||
| 62 | while (ptr->next != NULL){ | ||
| 63 | next = ptr->next; | ||
| 64 | free(ptr); | ||
| 65 | ptr = next; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | int update_node(node* ptr, int index, int value){ | ||
| 70 | /* | ||
| 71 | Update value of a node inside linked list | ||
| 72 | */ | ||
| 73 | // TODO:: What if the index is out of range? | ||
| 74 | |||
| 75 | if (index >= count_ll(ptr)){ | ||
| 76 | // index out of range | ||
| 77 | return 1; | ||
| 78 | } | ||
| 79 | |||
| 80 | int count = 0; | ||
| 81 | while (count < index){ | ||
| 82 | ptr = ptr->next; | ||
| 83 | count++; | ||
| 84 | } | ||
| 85 | |||
| 86 | ptr->value = value; | ||
| 87 | return 0; | ||
| 88 | } | ||
| 89 | |||
| 90 | int delete_node(node **ptr, int index){ | ||
| 91 | /* | ||
| 92 | Delete a node given the index | ||
| 93 | */ | ||
| 94 | |||
| 95 | // TODO:: What if the node doesn't exist? | ||
| 96 | node* prev; | ||
| 97 | if (index == 0){ | ||
| 98 | prev = *ptr; | ||
| 99 | *ptr = (*ptr)->next; | ||
| 100 | free(prev); | ||
| 101 | return 0; | ||
| 102 | } | ||
| 103 | |||
| 104 | node* root = *ptr; | ||
| 105 | for (int i = 0; i <= index; i++){ | ||
| 106 | if (i == index){ | ||
| 107 | prev->next = root->next; | ||
| 108 | free(root); | ||
| 109 | return 0; | ||
| 110 | } | ||
| 111 | prev = root; | ||
| 112 | root = root->next; | ||
| 113 | } | ||
| 114 | |||
| 115 | printf("Node not found\n"); | ||
| 116 | exit(-1); | ||
| 117 | } | ||
| 118 | |||
| 119 | int get_value(node *ptr, int index){ | ||
| 120 | /* | ||
| 121 | Get the value of a specific node given the index | ||
| 122 | */ | ||
| 123 | // TODO:: What if the index is out of range?? | ||
| 124 | |||
| 125 | for (int i = 0; i < index; i++){ | ||
| 126 | ptr = ptr->next; | ||
| 127 | } | ||
| 128 | return ptr->value; | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | node* search_node(node *ptr, int value){ | ||
| 133 | /* | ||
| 134 | Search for a node given the value and returns it | ||
| 135 | */ | ||
| 136 | |||
| 137 | while (1){ | ||
| 138 | if (ptr == NULL){ | ||
| 139 | fprintf(stderr, "Value doesn't exitst in linked list\n"); | ||
| 140 | exit(-1); | ||
| 141 | } | ||
| 142 | if (ptr->value == value){ | ||
| 143 | break; | ||
| 144 | } | ||
| 145 | ptr = ptr->next; | ||
| 146 | } | ||
| 147 | return ptr; | ||
| 148 | } | ||
| 149 | |||
| 150 | void reverse_ll(node** ptr){ | ||
| 151 | node *prev, *cur, *next; | ||
| 152 | int n = count_ll(*ptr); | ||
| 153 | |||
| 154 | if (n == 0){ | ||
| 155 | return; | ||
| 156 | } | ||
| 157 | |||
| 158 | if (n == 1){ | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | prev = *ptr; | ||
| 163 | cur = prev->next; | ||
| 164 | next = cur->next; | ||
| 165 | prev->next = NULL; | ||
| 166 | |||
| 167 | cur->next = prev; | ||
| 168 | |||
| 169 | for (int i = 0; i < n-2; i++){ | ||
| 170 | prev = cur; | ||
| 171 | cur = next; | ||
| 172 | next = next->next; | ||
| 173 | cur->next = prev; | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | *ptr = cur; | ||
| 178 | } | ||
| 179 | |||
