TP 9 exo2: fix a typo in a comment.
[TD_C.git] / TP_9 / exo2 / clist.c
CommitLineData
f8051b35 1#include <stdlib.h>
83ec54cd
JB
2#include <stdio.h>
3#include <stdbool.h>
f8051b35
JB
4
5#include "clist.h"
6
7link_t* list_new(int value) {
8f2b2843
JB
8 link_t* link_new;
9 link_new = malloc(sizeof(link_t));
10 link_new->value = value;
11 link_new->next = NULL;
12 return link_new;
f8051b35
JB
13}
14
15link_t* list_append(link_t* head, int value) {
16
17 if (head == NULL) {
18 return head = list_new(value);
19 } else {
20 link_t* head_first = head;
21 while (head->next != NULL) {
22 head = head->next;
23 }
24 head->next = list_new(value);
25 return head_first;
26 }
27}
28
29link_t* list_prepend(link_t* head, int value) {
30 link_t* first_link = list_new(value);
31
32 first_link->next = head;
33 return first_link;
34}
35
0d650b41 36link_t* list_insert(link_t* head, unsigned index, int value) {
0d650b41 37
3de00528
JB
38 if (index == 0) {
39 return list_prepend(head, value);
40 } else if (index == list_count(head)) {
41 return list_append(head, value);
42 } else {
43 link_t* link_insrt = list_new(value);
44 link_t* head_first = head;
45 link_t* head_next = NULL;
46 for (unsigned i = 0; i < index-1; i++) {
47 head = head->next;
48 }
49 head_next = head->next;
50 head->next = link_insrt;
51 head = link_insrt;
52 head->next = head_next;
53 return head_first;
0d650b41 54 }
0d650b41
JB
55}
56
57link_t* list_delete(link_t* head, unsigned index) {
0d650b41
JB
58 link_t* head_prev = NULL;
59 link_t* head_next = NULL;
74801358 60 link_t* head_ret = NULL;
0d650b41 61
abe54438
JB
62 if (head == NULL) {
63 return NULL;
64 } else if (index == 0) {
0d650b41
JB
65 head_next = head->next;
66 free(head);
67 head = head_next;
74801358 68 head_ret = head;
0d650b41 69 } else {
3de00528 70 link_t* head_first = head;
0d650b41
JB
71 for (unsigned i = 0; i < index-1; i++) {
72 head = head->next;
73 }
74 head_prev = head;
75 head = head->next;
76 head_next = head->next;
77 free(head);
78 head = head_prev;
79 head->next = head_next;
74801358
JB
80 head_ret = head_first;
81 }
82 if (head_ret != NULL) {
83 return head_ret;
84 } else {
85 return NULL;
0d650b41
JB
86 }
87}
88
970a0122
JB
89link_t* list_concat(link_t* first, link_t* second) {
90 link_t* head_first = first;
91
92 while (first->next != NULL) {
93 first = first->next;
94 }
95 first->next = second;
96 return head_first;
97}
98
83ec54cd
JB
99link_t* list_sort(link_t* head) {
100 int tmp;
101 bool isswaped;
102 link_t* head_first = head;
103
104 do {
105 isswaped = false;
106 while (head->next != NULL) {
107 if (head->value > head->next->value) {
108 tmp = head->value;
109 head->value = head->next->value;
110 head->next->value = tmp;
111 isswaped = true;
112 }
113 head = head->next;
114 }
954256b7 115 /* Reloop at the beginning of the list until there's values swaped */
83ec54cd
JB
116 head = head_first;
117 } while (isswaped);
118 return head_first;
119}
120
f8051b35 121unsigned list_count(link_t* head) {
65544a82 122 unsigned count = 0;
f8051b35 123
8f2b2843 124 while (head != NULL) {
f8051b35
JB
125 ++count;
126 head = head->next;
127 }
128 return count;
129}
130
131void list_set(link_t* head, unsigned index, int value) {
8f2b2843 132 unsigned count = 0;
f8051b35 133
8f2b2843
JB
134 while (head != NULL && count < index) {
135 ++count;
f8051b35
JB
136 head = head->next;
137 }
8f2b2843 138 if (head != NULL) { head->value = value; }
f8051b35
JB
139}
140
141int list_get(link_t* head, unsigned index) {
8f2b2843
JB
142 unsigned count = 0;
143
144 while (head != NULL && count < index) {
145 ++count;
146 head = head->next;
147 }
148 if (head != NULL) {
f8051b35 149 return head->value;
8f2b2843 150 } else {
f8051b35
JB
151 return -1;
152 }
153}
154
74801358 155void list_clear(link_t* head) {
8f2b2843 156 link_t* next_link = NULL;
f8051b35 157
74801358
JB
158 while (head != NULL) {
159 next_link = head->next;
160 free(head);
161 head = next_link;
f8051b35
JB
162 }
163}
83ec54cd
JB
164
165void list_display_values(link_t* head) {
65544a82 166 unsigned i = 0;
83ec54cd
JB
167
168 printf("------Begin------\n");
169 while (head != NULL) {
170 printf("value at [%d]=%d\n", i, head->value);
171 head = head->next;
172 i++;
173 }
174 printf("------End------\n");
175}