TP 9 exo2: Better handling of special cases in list_delete()
[TD_C.git] / TP_9 / exo2 / clist.c
CommitLineData
f8051b35
JB
1#include <stdlib.h>
2
3#include "clist.h"
4
5link_t* list_new(int value) {
8f2b2843
JB
6 link_t* link_new;
7 link_new = malloc(sizeof(link_t));
8 link_new->value = value;
9 link_new->next = NULL;
10 return link_new;
f8051b35
JB
11}
12
13link_t* list_append(link_t* head, int value) {
14
15 if (head == NULL) {
16 return head = list_new(value);
17 } else {
18 link_t* head_first = head;
19 while (head->next != NULL) {
20 head = head->next;
21 }
22 head->next = list_new(value);
23 return head_first;
24 }
25}
26
27link_t* list_prepend(link_t* head, int value) {
28 link_t* first_link = list_new(value);
29
30 first_link->next = head;
31 return first_link;
32}
33
0d650b41 34link_t* list_insert(link_t* head, unsigned index, int value) {
0d650b41 35
3de00528
JB
36 if (index == 0) {
37 return list_prepend(head, value);
38 } else if (index == list_count(head)) {
39 return list_append(head, value);
40 } else {
41 link_t* link_insrt = list_new(value);
42 link_t* head_first = head;
43 link_t* head_next = NULL;
44 for (unsigned i = 0; i < index-1; i++) {
45 head = head->next;
46 }
47 head_next = head->next;
48 head->next = link_insrt;
49 head = link_insrt;
50 head->next = head_next;
51 return head_first;
0d650b41 52 }
0d650b41
JB
53}
54
55link_t* list_delete(link_t* head, unsigned index) {
0d650b41
JB
56 link_t* head_prev = NULL;
57 link_t* head_next = NULL;
74801358 58 link_t* head_ret = NULL;
0d650b41 59
abe54438
JB
60 if (head == NULL) {
61 return NULL;
62 } else if (index == 0) {
0d650b41
JB
63 head_next = head->next;
64 free(head);
65 head = head_next;
74801358 66 head_ret = head;
0d650b41 67 } else {
3de00528 68 link_t* head_first = head;
0d650b41
JB
69 for (unsigned i = 0; i < index-1; i++) {
70 head = head->next;
71 }
72 head_prev = head;
73 head = head->next;
74 head_next = head->next;
75 free(head);
76 head = head_prev;
77 head->next = head_next;
74801358
JB
78 head_ret = head_first;
79 }
80 if (head_ret != NULL) {
81 return head_ret;
82 } else {
83 return NULL;
0d650b41
JB
84 }
85}
86
f8051b35 87unsigned list_count(link_t* head) {
490c6927 88 int count = 0;
f8051b35 89
8f2b2843 90 while (head != NULL) {
f8051b35
JB
91 ++count;
92 head = head->next;
93 }
94 return count;
95}
96
97void list_set(link_t* head, unsigned index, int value) {
8f2b2843 98 unsigned count = 0;
f8051b35 99
8f2b2843
JB
100 while (head != NULL && count < index) {
101 ++count;
f8051b35
JB
102 head = head->next;
103 }
8f2b2843 104 if (head != NULL) { head->value = value; }
f8051b35
JB
105}
106
107int list_get(link_t* head, unsigned index) {
8f2b2843
JB
108 unsigned count = 0;
109
110 while (head != NULL && count < index) {
111 ++count;
112 head = head->next;
113 }
114 if (head != NULL) {
f8051b35 115 return head->value;
8f2b2843 116 } else {
f8051b35
JB
117 return -1;
118 }
119}
120
74801358 121void list_clear(link_t* head) {
8f2b2843 122 link_t* next_link = NULL;
f8051b35 123
74801358
JB
124 while (head != NULL) {
125 next_link = head->next;
126 free(head);
127 head = next_link;
f8051b35
JB
128 }
129}