Created
January 4, 2018 17:57
-
-
Save Romain-P/7d97dc2ff7b06e2ce01166a8d9ae326a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include "simple_list.h" | |
| unsigned int list_get_size(list_t list) | |
| { | |
| int i = 0; | |
| if (list) | |
| i = 1; | |
| else | |
| return (0); | |
| while (list->next) { | |
| ++i; | |
| list = list->next; | |
| } | |
| return (i); | |
| } | |
| bool_t list_is_empty(list_t list) | |
| { | |
| return (list == NULL ? TRUE : FALSE); | |
| } | |
| void list_dump(list_t list) | |
| { | |
| while (list) { | |
| printf("%f\n", list->value); | |
| list = list->next; | |
| } | |
| } | |
| bool_t list_add_elem_at_front(list_t *front_ptr, double elem) | |
| { | |
| node_t *node; | |
| if (!front_ptr) | |
| return (FALSE); | |
| node = malloc(sizeof(node_t)); | |
| if (node) { | |
| node->next = *front_ptr; | |
| node->value = elem; | |
| *front_ptr = node; | |
| } | |
| return (node ? TRUE : FALSE); | |
| } | |
| bool_t list_add_elem_at_back(list_t *front_ptr, double elem) | |
| { | |
| node_t *node; | |
| if (!front_ptr) | |
| return (FALSE); | |
| else if (!(*front_ptr)) { | |
| node = malloc(sizeof(list_t)); | |
| node->value = elem; | |
| node->next = NULL; | |
| *front_ptr = node; | |
| } else { | |
| node = *front_ptr; | |
| while (node->next) | |
| node = node->next; | |
| node->next = malloc(sizeof(node_t)); | |
| if (node->next) { | |
| node->next->value = elem; | |
| node->next->next = NULL; | |
| } | |
| } | |
| return (node ? TRUE : FALSE); | |
| } | |
| bool_t list_add_elem_at_position(list_t *front_ptr, double elem, unsigned int position) | |
| { | |
| node_t *node; | |
| if (!front_ptr) | |
| return (FALSE); | |
| node = *front_ptr; | |
| for (size_t i = 0; i < position; ++i) { | |
| if (!node || !node->next) | |
| return (FALSE); | |
| node = node->next; | |
| } | |
| return (list_add_elem_at_front(&node, elem)); | |
| } | |
| bool_t list_del_elem_at_front(list_t *front_ptr) | |
| { | |
| if (!front_ptr || !(*front_ptr)) | |
| return (FALSE); | |
| *front_ptr = (*front_ptr)->next; | |
| return (TRUE); | |
| } | |
| bool_t list_del_elem_at_position(list_t *front_ptr, unsigned int position) | |
| { | |
| node_t *node; | |
| if (!front_ptr || !(*front_ptr)) | |
| return (FALSE); | |
| node = *front_ptr; | |
| for (size_t i = 0; i < position - 1; ++i) { | |
| if (!node->next) | |
| return (FALSE); | |
| node = node->next; | |
| } | |
| node->next = node->next->next; | |
| return (TRUE); | |
| } | |
| bool_t list_del_elem_at_back(list_t *front_ptr) | |
| { | |
| node_t *node; | |
| node_t *cache; | |
| if (!front_ptr || !(*front_ptr)) | |
| return (FALSE); | |
| node = *front_ptr; | |
| while (node) | |
| { | |
| if (!node->next) | |
| cache->next = NULL; | |
| cache = node; | |
| node = node->next; | |
| } | |
| return (FALSE); | |
| } | |
| double list_get_elem_at_front(list_t list) | |
| { | |
| return (list ? list->value : 0); | |
| } | |
| double list_get_elem_at_back(list_t list) | |
| { | |
| if (!list) | |
| return (0); | |
| while (list->next) | |
| list = list->next; | |
| return (list->value); | |
| } | |
| double list_get_elem_at_position(list_t list, unsigned int position) | |
| { | |
| if (!list) | |
| return (0); | |
| for (size_t i = 0; i < position; ++i) { | |
| if (!list->next) | |
| return (0); | |
| list = list->next; | |
| } | |
| return (list->value); | |
| } | |
| node_t *list_get_first_node_with_value(list_t list, double value) | |
| { | |
| if (!list) | |
| return (NULL); | |
| list->value = value; | |
| return (list); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment