Cleanups.
[TD_C.git] / TP_9 / exo1 / clist.c
CommitLineData
1c7a8fe9
JB
1#include <stdlib.h>
2
3#include "clist.h"
4
5link_t* list_new(int value) {
6 link_t* link_t_new;
7 link_t_new = malloc(sizeof(link_t));
8 link_t_new->value = value;
9 link_t_new->next = NULL;
10 return link_t_new;
11}
12
13void list_append(link_t* head, int value) {
14
15 while (head->next != NULL) {
16 head = head->next;
17 }
18 head->next = list_new(value);
19}
20
21unsigned list_count(link_t* head) {
22 // And if head is not defined?
23 int count = 1;
24
25 while (head->next != NULL) {
26 ++count;
27 head = head->next;
28 }
29 return count;
30}
31
32int list_get(link_t* head, unsigned index) {
33
34 if (index < list_count(head)) {
35 for (unsigned i = 0; i < index; i++) {
36 head = head->next;
37 }
38 return head->value;
39 } else {
40 return -1;
41 }
42}
43
44void list_clear(link_t* link) {
45
46 while (link != NULL) {
5dd62128 47 link_t* next_link = link->next;
1c7a8fe9
JB
48 free(link);
49 link = next_link;
50 }
51}