TP 9 exo2: Add link_sort linked list helper function
authorJerome Benoit <jerome.benoit@sap.com>
Fri, 3 Mar 2017 21:45:07 +0000 (22:45 +0100)
committerJerome Benoit <jerome.benoit@sap.com>
Fri, 3 Mar 2017 21:45:07 +0000 (22:45 +0100)
The sorting algorithm is bubble sort.

Signed-off-by: Jerome Benoit <jerome.benoit@sap.com>
TP_9/exo2/clist.c
TP_9/exo2/clist.h
TP_9/exo2/exo2.c

index 9ebb34817e5982fa3b3131e388d01b320f6a3a70..100f77dae4d59fe4493988aba4ad06d1c005970a 100644 (file)
@@ -1,4 +1,6 @@
 #include <stdlib.h>
 #include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
 
 #include "clist.h"
 
 
 #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;
     
 unsigned list_count(link_t* head) {
     int count = 0;
     
@@ -127,3 +151,15 @@ void list_clear(link_t* head) {
         head = next_link;
     }
 }
         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");
+}
index 40eaef10f367d4284b4bccdc2c8f1753e06a2e50..5c3a68fd577019577c5247154e20f0b454f79891 100644 (file)
@@ -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_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);
 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 */
 
 #endif /* CLIST_H */
index 3b62ea4620b90f7ffa7dffa6307d15163391a350..1b243af4d849557d760b35be5e0695f72fedaed1 100644 (file)
@@ -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));
     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));
     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);
     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);
     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);
     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;
     list_clear(head);
 
     return 0;