diff options
author | Franciszek Malinka <franciszek.malinka@gmail.com> | 2021-10-05 21:49:54 +0200 |
---|---|---|
committer | Franciszek Malinka <franciszek.malinka@gmail.com> | 2021-10-05 21:49:54 +0200 |
commit | c5fcf7179a83ef65c86c6a4a390029149e518649 (patch) | |
tree | d29ffc5b86a0d257453cedcf87d91a13d8bf3b0d /semestr-4/sieci/pracownia2/router/linked_list.c | |
parent | f8a88b6a4aba1f66d04711a9330eaba49a50c463 (diff) |
Duzy commit ze smieciami
Diffstat (limited to 'semestr-4/sieci/pracownia2/router/linked_list.c')
-rw-r--r-- | semestr-4/sieci/pracownia2/router/linked_list.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/semestr-4/sieci/pracownia2/router/linked_list.c b/semestr-4/sieci/pracownia2/router/linked_list.c new file mode 100644 index 0000000..16113ac --- /dev/null +++ b/semestr-4/sieci/pracownia2/router/linked_list.c @@ -0,0 +1,79 @@ +/* + * Program: router + * Autor: Franciszek Malinka, 316093 + */ + +#include "linked_list.h" +#include <stdlib.h> +#include <stdint.h> + +node_t *_next(node_t *node) { + return (node == NULL) ? NULL : node->next; +} + +void _insert(node_t **head, void *data, size_t data_size) { + node_t *new_node = (node_t *)malloc(sizeof(node_t)); + new_node->data = malloc(data_size); + for (int i = 0; i < data_size; i++) + *(uint8_t *)(new_node->data + i) = *(uint8_t *)(data + i); + new_node->next = *head; + *head = new_node; +} + +void _free_node(node_t *node) { + free(node->data); + free(node); +} + +void _erase(node_t **head) { + node_t *next_node = _next(*head); + _free_node(*head); + *head = next_node; +} + +void _free_list(node_t *head) { + if (head == NULL) return; + _free_list(head->next); + _free_node(head); +} + +list_t create_list() { + list_t ret; + ret.head = NULL; + ret.it = NULL; + return ret; +} + +void insert(list_t *list, void *data, size_t data_size) { + _insert(&list->head, data, data_size); +} + +void erase(list_t *list) { + _erase(&list->head); +} + +void erase_it(list_t *list) { + if(list->it == NULL) return; + if(list->prev_it == NULL) { + erase(list); + reset(list); + return; + } + list->prev_it->next = _next(list->it); + _free_node(list->it); + list->it = list->prev_it->next; +} + +void iterate(list_t *list) { + list->prev_it = list->it; + list->it = _next(list->it); +} + +void reset(list_t *list) { + list->prev_it = NULL; + list->it = list->head; +} + +void free_list(list_t *list) { + _free_list(list->head); +}
\ No newline at end of file |