// // Created by kubik on 7 lip 2018. // #include #include #include "linked_list.h" struct linked_list_t *ll_create() { return malloc(sizeof(List)); } int ll_push_back(struct linked_list_t *ll, int value) { if (ll == NULL) return 1; Node *n = malloc(sizeof(Node)); if (n == NULL) { return 2; } n->data = value; n->next = NULL; if (ll->head == NULL) { ll->head = n; ll->tail = n; } else { ll->tail->next = n; ll->tail = n; } return 0; } int ll_push_front(struct linked_list_t *ll, int value) { if (ll == NULL) return 1; Node *n = malloc(sizeof(Node)); if (n == NULL) { return 2; } n->data = value; if (ll->head == NULL) { ll->head = n; ll->tail = n; n->next = NULL; } else { n->next = ll->head; ll->head = n; } return 0; } int ll_pop_front(struct linked_list_t *ll, int *err_code) { if (ll == NULL || ll->head == NULL) { if (err_code != NULL) { *err_code = 1; } return 0; } if (err_code != NULL) { *err_code = 0; } int ret = ll->head->data; Node *n = ll->head; ll->head = ll->head->next; free(n); if (ll->head == NULL) { ll->tail = NULL; } return ret; } int ll_pop_back(struct linked_list_t *ll, int *err_code) { if (ll == NULL || ll->head == NULL) { if (err_code != NULL) { *err_code = 1; } return 0; } if (err_code != NULL) { *err_code = 0; } int ret = ll->tail->data; if (ll->head == ll->tail) { free(ll->head); ll->head = NULL; ll->tail = NULL; return ret; } Node *n = ll->head; for (; n->next != ll->tail; n = n->next); free(ll->tail); ll->tail = n; n->next = NULL; return ret; } int ll_back(const struct linked_list_t *ll, int *err_code) { if (ll == NULL || ll->head == NULL) { if (err_code != NULL) { *err_code = 1; } return 0; } if (err_code != NULL) { *err_code = 0; } return ll->tail->data; } int ll_front(const struct linked_list_t *ll, int *err_code) { if (ll == NULL || ll->head == NULL) { if (err_code != NULL) { *err_code = 1; } return 0; } if (err_code != NULL) { *err_code = 0; } return ll->head->data; } struct node_t *ll_begin(struct linked_list_t *ll) { if (ll == NULL || ll->head == NULL) { return NULL; } return ll->head; } struct node_t *ll_end(struct linked_list_t *ll) { if (ll == NULL || ll->head == NULL) { return NULL; } return ll->tail; } int ll_size(const struct linked_list_t *ll) { if (ll == NULL) { return -1; } if (ll->head == NULL) { return 0; } int i; Node *n = ll->head; for (i = 1; n != ll->tail; n = n->next, i++); return i; } int ll_is_empty(const struct linked_list_t *ll) { if (ll == NULL) { return -1; } if (ll->head == NULL) { return 1; } return 0; } int ll_at(const struct linked_list_t *ll, unsigned int index, int *err_code) { if (ll == NULL || ll->head == NULL) { if (err_code != NULL) { *err_code = 1; } return 0; } if (err_code != NULL) { *err_code = 0; } Node *n = ll->head; unsigned int i; for (i = 0; i < index; i++) { n = n->next; if (n == NULL) { if (err_code) { *err_code = 1; } return 0; } } return n->data; } int ll_insert(struct linked_list_t *ll, unsigned int index, int value) { if (ll == NULL || index > (unsigned int) ll_size(ll)) { return 1; } Node *n = malloc(sizeof(Node)); if (n == NULL) { return 2; } n->data = value; if (index == 0) { n->next = ll->head; ll->head = n; if (ll->tail == NULL) { ll->tail = n; } return 0; } if (index == (unsigned int) ll_size(ll)) { n->next = NULL; ll->tail->next = n; ll->tail = n; return 0; } Node *node = ll->head; unsigned int i; for (i = 0; i < index - 1; i++) { node = node->next; } n->next = node->next; node->next = n; if (ll->tail == node) { ll->tail = n; } return 0; } int ll_remove(struct linked_list_t *ll, unsigned int index, int *err_code) { if (ll == NULL || ll->head == NULL || index >= (unsigned int) ll_size(ll)) { if (err_code != NULL) { *err_code = 1; } return 0; } if (err_code != NULL) { *err_code = 0; } Node *node = ll->head; int ret; if (index == 0) { if (ll->tail == ll->head) { ll->tail = NULL; } ll->head = ll->head->next; ret = node->data; free(node); return ret; } unsigned int i; for (i = 0; i < index - 1; i++) { node = node->next; } Node *next = node->next; if (next->next == NULL) { ll->tail = node; } node->next = node->next->next; ret = next->data; free(next); return ret; } void ll_clear(struct linked_list_t *ll) { if (ll == NULL || ll->head == NULL) { return; } Node *node = ll->head; ll->head = NULL; ll->tail = NULL; Node *next = node; while (next != NULL) { next = node->next; free(node); node = next; } } void ll_display(const struct linked_list_t *ll) { if (ll == NULL || ll->head == NULL) { return; } Node *node = ll->head; while (node != NULL) { printf("%d ", node->data); node = node->next; } }