From 83ec54cdf5ddd1092ed4ba11d58fec8a0db07b68 Mon Sep 17 00:00:00 2001 From: Jerome Benoit Date: Fri, 3 Mar 2017 22:45:07 +0100 Subject: [PATCH] TP 9 exo2: Add link_sort linked list helper function The sorting algorithm is bubble sort. Signed-off-by: Jerome Benoit --- TP_9/exo2/clist.c | 36 ++++++++++++++++++++++++++++++++++++ TP_9/exo2/clist.h | 4 +++- TP_9/exo2/exo2.c | 28 +++++++++++----------------- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/TP_9/exo2/clist.c b/TP_9/exo2/clist.c index 9ebb348..100f77d 100644 --- a/TP_9/exo2/clist.c +++ b/TP_9/exo2/clist.c @@ -1,4 +1,6 @@ #include +#include +#include #include "clist.h" @@ -84,6 +86,28 @@ link_t* list_delete(link_t* head, unsigned index) { } } +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; @@ -127,3 +151,15 @@ void list_clear(link_t* head) { 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"); +} diff --git a/TP_9/exo2/clist.h b/TP_9/exo2/clist.h index 40eaef1..5c3a68f 100644 --- a/TP_9/exo2/clist.h +++ b/TP_9/exo2/clist.h @@ -12,9 +12,11 @@ link_t* list_append(link_t* head, int value); 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 */ diff --git a/TP_9/exo2/exo2.c b/TP_9/exo2/exo2.c index 3b62ea4..1b243af 100644 --- a/TP_9/exo2/exo2.c +++ b/TP_9/exo2/exo2.c @@ -10,28 +10,22 @@ int main() { 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; -- 2.34.1