X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TP_9%2Fexo1%2Fclist.c;fp=TP_9%2Fexo1%2Fclist.c;h=69fd8ccb74ffc088389398e7fad59a3a9e8eab4e;hb=1c7a8fe964e67bfcdc3e6550d0a090e8686e8387;hp=0000000000000000000000000000000000000000;hpb=fab9c01b7870244a46f86b79d925e5ada1aea243;p=TD_C.git diff --git a/TP_9/exo1/clist.c b/TP_9/exo1/clist.c new file mode 100644 index 0000000..69fd8cc --- /dev/null +++ b/TP_9/exo1/clist.c @@ -0,0 +1,51 @@ +#include + +#include "clist.h" + +link_t* list_new(int value) { + link_t* link_t_new; + link_t_new = malloc(sizeof(link_t)); + link_t_new->value = value; + link_t_new->next = NULL; + return link_t_new; +} + +void list_append(link_t* head, int value) { + + while (head->next != NULL) { + head = head->next; + } + head->next = list_new(value); +} + +unsigned list_count(link_t* head) { + // And if head is not defined? + int count = 1; + + while (head->next != NULL) { + ++count; + head = head->next; + } + return count; +} + +int list_get(link_t* head, unsigned index) { + + if (index < list_count(head)) { + for (unsigned i = 0; i < index; i++) { + head = head->next; + } + return head->value; + } else { + return -1; + } +} + +void list_clear(link_t* link) { + + while (link != NULL) { + link_t* next_link = link; + free(link); + link = next_link; + } +}