| 1 | #include <stdlib.h> |
| 2 | |
| 3 | #include "clist.h" |
| 4 | |
| 5 | link_t* list_new(int value) { |
| 6 | link_t* link_new; |
| 7 | link_new = malloc(sizeof(link_t)); |
| 8 | link_new->value = value; |
| 9 | link_new->next = NULL; |
| 10 | return link_new; |
| 11 | } |
| 12 | |
| 13 | link_t* list_append(link_t* head, int value) { |
| 14 | |
| 15 | if (head == NULL) { |
| 16 | return head = list_new(value); |
| 17 | } else { |
| 18 | link_t* head_first = head; |
| 19 | while (head->next != NULL) { |
| 20 | head = head->next; |
| 21 | } |
| 22 | head->next = list_new(value); |
| 23 | return head_first; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | link_t* list_prepend(link_t* head, int value) { |
| 28 | link_t* first_link = list_new(value); |
| 29 | |
| 30 | first_link->next = head; |
| 31 | return first_link; |
| 32 | } |
| 33 | |
| 34 | link_t* list_insert(link_t* head, unsigned index, int value) { |
| 35 | |
| 36 | if (index == 0) { |
| 37 | return list_prepend(head, value); |
| 38 | } else if (index == list_count(head)) { |
| 39 | return list_append(head, value); |
| 40 | } else { |
| 41 | link_t* link_insrt = list_new(value); |
| 42 | link_t* head_first = head; |
| 43 | link_t* head_next = NULL; |
| 44 | for (unsigned i = 0; i < index-1; i++) { |
| 45 | head = head->next; |
| 46 | } |
| 47 | head_next = head->next; |
| 48 | head->next = link_insrt; |
| 49 | head = link_insrt; |
| 50 | head->next = head_next; |
| 51 | return head_first; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | link_t* list_delete(link_t* head, unsigned index) { |
| 56 | link_t* head_prev = NULL; |
| 57 | link_t* head_next = NULL; |
| 58 | link_t* head_ret = NULL; |
| 59 | |
| 60 | if (index == 0) { |
| 61 | head_next = head->next; |
| 62 | free(head); |
| 63 | head = head_next; |
| 64 | head_ret = head; |
| 65 | } else { |
| 66 | link_t* head_first = head; |
| 67 | for (unsigned i = 0; i < index-1; i++) { |
| 68 | head = head->next; |
| 69 | } |
| 70 | head_prev = head; |
| 71 | head = head->next; |
| 72 | head_next = head->next; |
| 73 | free(head); |
| 74 | head = head_prev; |
| 75 | head->next = head_next; |
| 76 | head_ret = head_first; |
| 77 | } |
| 78 | if (head_ret != NULL) { |
| 79 | return head_ret; |
| 80 | } else { |
| 81 | return NULL; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | unsigned list_count(link_t* head) { |
| 86 | int count = 0; |
| 87 | |
| 88 | while (head != NULL) { |
| 89 | ++count; |
| 90 | head = head->next; |
| 91 | } |
| 92 | return count; |
| 93 | } |
| 94 | |
| 95 | void list_set(link_t* head, unsigned index, int value) { |
| 96 | unsigned count = 0; |
| 97 | |
| 98 | while (head != NULL && count < index) { |
| 99 | ++count; |
| 100 | head = head->next; |
| 101 | } |
| 102 | if (head != NULL) { head->value = value; } |
| 103 | } |
| 104 | |
| 105 | int list_get(link_t* head, unsigned index) { |
| 106 | unsigned count = 0; |
| 107 | |
| 108 | while (head != NULL && count < index) { |
| 109 | ++count; |
| 110 | head = head->next; |
| 111 | } |
| 112 | if (head != NULL) { |
| 113 | return head->value; |
| 114 | } else { |
| 115 | return -1; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void list_clear(link_t* head) { |
| 120 | link_t* next_link = NULL; |
| 121 | |
| 122 | while (head != NULL) { |
| 123 | next_link = head->next; |
| 124 | free(head); |
| 125 | head = next_link; |
| 126 | } |
| 127 | } |