#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
#include "clist.h"
}
}
+link_t* list_sort(link_t* head) {
+ int tmp;
+ bool isswaped;
+ link_t* head_first = head;
+
+ do {
+ isswaped = false;
+ while (head->next != NULL) {
+ if (head->value > head->next->value) {
+ tmp = head->value;
+ head->value = head->next->value;
+ head->next->value = tmp;
+ isswaped = true;
+ }
+ head = head->next;
+ }
+ /* Reloop at the beginning of the list until there's is values swaped */
+ head = head_first;
+ } while (isswaped);
+ return head_first;
+}
+
unsigned list_count(link_t* head) {
int count = 0;
head = next_link;
}
}
+
+void list_display_values(link_t* head) {
+ int i = 0;
+
+ printf("------Begin------\n");
+ while (head != NULL) {
+ printf("value at [%d]=%d\n", i, head->value);
+ head = head->next;
+ i++;
+ }
+ printf("------End------\n");
+}
link_t* list_prepend(link_t* head, int value);
link_t* list_insert(link_t* head, unsigned index, int value);
link_t* list_delete(link_t* head, unsigned index);
+link_t* list_sort(link_t* head);
unsigned list_count(link_t* head);
void list_set(link_t* head, unsigned index, int value);
int list_get(link_t* head, unsigned index);
-void list_clear(link_t* link);
+void list_clear(link_t* head);
+void list_display_values(link_t* head);
#endif /* CLIST_H */
head = list_append(head, 3);
head = list_append(head, 4);
printf("Longueur de la liste: %d\n", list_count(head));
- printf("Valeur a index %d: %d\n", 0, list_get(head, 0));
- printf("Valeur a index %d: %d\n", 1, list_get(head, 1));
- printf("Valeur a index %d: %d\n", 2, list_get(head, 2));
- printf("Valeur a index %d: %d\n", 3, list_get(head, 3));
+ list_display_values(head);
head = list_prepend(head, 5);
printf("Longueur de la liste: %d\n", list_count(head));
- printf("Valeur a index %d: %d\n", 0, list_get(head, 0));
- printf("Valeur a index %d: %d\n", 4, list_get(head, 4));
+ list_display_values(head);
list_set(head, 0, 78);
- printf("Valeur a index %d: %d\n", 0, list_get(head, 0));
- printf("Valeur a index %d: %d\n", 1, list_get(head, 1));
- printf("Valeur a index %d: %d\n", 2, list_get(head, 2));
+ list_display_values(head);
head = list_insert(head, 2, 7);
- printf("Valeur a index %d: %d\n", 1, list_get(head, 1));
- printf("Valeur a index %d: %d\n", 2, list_get(head, 2));
- printf("Valeur a index %d: %d\n", 3, list_get(head, 3));
+ list_display_values(head);
head = list_delete(head, 3);
- printf("Valeur a index %d: %d\n", 0, list_get(head, 0));
- printf("Valeur a index %d: %d\n", 1, list_get(head, 1));
- printf("Valeur a index %d: %d\n", 2, list_get(head, 2));
- printf("Valeur a index %d: %d\n", 3, list_get(head, 3));
- printf("Valeur a index %d: %d\n", 4, list_get(head, 4));
+ list_display_values(head);
+ head = list_append(head, 5);
+ head = list_append(head, 12);
+ head = list_append(head, 65);
+ head = list_append(head, 21);
+ head = list_sort(head);
+ list_display_values(head);
list_clear(head);
return 0;