100f77dae4d59fe4493988aba4ad06d1c005970a
[TD_C.git] / TP_9 / exo2 / clist.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdbool.h>
4
5 #include "clist.h"
6
7 link_t* list_new(int value) {
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;
13 }
14
15 link_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
29 link_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
36 link_t* list_insert(link_t* head, unsigned index, int value) {
37
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;
54 }
55 }
56
57 link_t* list_delete(link_t* head, unsigned index) {
58 link_t* head_prev = NULL;
59 link_t* head_next = NULL;
60 link_t* head_ret = NULL;
61
62 if (head == NULL) {
63 return NULL;
64 } else if (index == 0) {
65 head_next = head->next;
66 free(head);
67 head = head_next;
68 head_ret = head;
69 } else {
70 link_t* head_first = head;
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;
80 head_ret = head_first;
81 }
82 if (head_ret != NULL) {
83 return head_ret;
84 } else {
85 return NULL;
86 }
87 }
88
89 link_t* list_sort(link_t* head) {
90 int tmp;
91 bool isswaped;
92 link_t* head_first = head;
93
94 do {
95 isswaped = false;
96 while (head->next != NULL) {
97 if (head->value > head->next->value) {
98 tmp = head->value;
99 head->value = head->next->value;
100 head->next->value = tmp;
101 isswaped = true;
102 }
103 head = head->next;
104 }
105 /* Reloop at the beginning of the list until there's is values swaped */
106 head = head_first;
107 } while (isswaped);
108 return head_first;
109 }
110
111 unsigned list_count(link_t* head) {
112 int count = 0;
113
114 while (head != NULL) {
115 ++count;
116 head = head->next;
117 }
118 return count;
119 }
120
121 void list_set(link_t* head, unsigned index, int value) {
122 unsigned count = 0;
123
124 while (head != NULL && count < index) {
125 ++count;
126 head = head->next;
127 }
128 if (head != NULL) { head->value = value; }
129 }
130
131 int list_get(link_t* head, unsigned index) {
132 unsigned count = 0;
133
134 while (head != NULL && count < index) {
135 ++count;
136 head = head->next;
137 }
138 if (head != NULL) {
139 return head->value;
140 } else {
141 return -1;
142 }
143 }
144
145 void list_clear(link_t* head) {
146 link_t* next_link = NULL;
147
148 while (head != NULL) {
149 next_link = head->next;
150 free(head);
151 head = next_link;
152 }
153 }
154
155 void list_display_values(link_t* head) {
156 int i = 0;
157
158 printf("------Begin------\n");
159 while (head != NULL) {
160 printf("value at [%d]=%d\n", i, head->value);
161 head = head->next;
162 i++;
163 }
164 printf("------End------\n");
165 }