TP 9 exo2: Add list_concat() linked list helper function.
authorJerome Benoit <jerome.benoit@sap.com>
Fri, 3 Mar 2017 22:48:46 +0000 (23:48 +0100)
committerJerome Benoit <jerome.benoit@sap.com>
Fri, 3 Mar 2017 22:48:46 +0000 (23:48 +0100)
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 100f77dae4d59fe4493988aba4ad06d1c005970a..4812ab8d724d9d99c6760c86cf786fd9c8b5bb91 100644 (file)
@@ -86,6 +86,16 @@ link_t* list_delete(link_t* head, unsigned index) {
     }
 }
 
     }
 }
 
+link_t* list_concat(link_t* first, link_t* second) {
+    link_t* head_first = first;
+
+    while (first->next != NULL) {
+        first = first->next;
+    }
+    first->next = second;
+    return head_first;
+}
+
 link_t* list_sort(link_t* head) {
     int tmp;
     bool isswaped;
 link_t* list_sort(link_t* head) {
     int tmp;
     bool isswaped;
index 5c3a68fd577019577c5247154e20f0b454f79891..a08ec178c103e95017254c60cbf943ff7db82960 100644 (file)
@@ -12,6 +12,7 @@ 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_concat(link_t* first, link_t* second);
 link_t* list_sort(link_t* head);
 unsigned list_count(link_t* head);
 void list_set(link_t* head, unsigned index, int value);
 link_t* list_sort(link_t* head);
 unsigned list_count(link_t* head);
 void list_set(link_t* head, unsigned index, int value);
index 1b243af4d849557d760b35be5e0695f72fedaed1..09855d53fe2250013dbff166af5b9b2399e61bfc 100644 (file)
@@ -3,29 +3,40 @@
 #include "clist.h"
 
 int main() {
 #include "clist.h"
 
 int main() {
+    link_t* head1 = NULL;
+    link_t* head2 = NULL;
     link_t* head = NULL;
     link_t* head = NULL;
-    printf("Longueur de la liste: %d\n", list_count(head));
-    head = list_append(head, 1);
-    head = list_append(head, 2);
-    head = list_append(head, 3);
-    head = list_append(head, 4);
-    printf("Longueur de la liste: %d\n", list_count(head));
-    list_display_values(head);
-    head = list_prepend(head, 5);
-    printf("Longueur de la liste: %d\n", list_count(head));
-    list_display_values(head);
-    list_set(head, 0, 78);
-    list_display_values(head);
-    head = list_insert(head, 2, 7);
-    list_display_values(head);
-    head = list_delete(head, 3);
-    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);
+
+    printf("Longueur de la liste: %d\n", list_count(head1));
+    head1 = list_append(head1, 1);
+    head1 = list_append(head1, 2);
+    head1 = list_append(head1, 3);
+    head1 = list_append(head1, 4);
+    printf("Longueur de la liste: %d\n", list_count(head1));
+    list_display_values(head1);
+    head1 = list_prepend(head1, 5);
+    printf("Longueur de la liste: %d\n", list_count(head1));
+    list_display_values(head1);
+    list_set(head1, 0, 78);
+    list_display_values(head1);
+    head1 = list_insert(head1, 2, 7);
+    list_display_values(head1);
+    head1 = list_delete(head1, 3);
+    list_display_values(head1);
+    head1 = list_append(head1, 5);
+    head1 = list_append(head1, 12);
+    head1 = list_append(head1, 65);
+    head1 = list_append(head1, 21);
+    head1 = list_sort(head1);
+    list_display_values(head1);
+    head2 = list_insert(head2, 0, 8);
+    head2 = list_append(head2, 6);
+    head2 = list_prepend(head2, 5);
+    list_display_values(head2);
+    head = list_concat(head1, head2);
     list_display_values(head);
     list_display_values(head);
+    //list_clear(head1);
+    //list_clear(head2);
     list_clear(head);
 
     return 0;
     list_clear(head);
 
     return 0;