From: Jerome Benoit Date: Fri, 3 Mar 2017 22:48:46 +0000 (+0100) Subject: TP 9 exo2: Add list_concat() linked list helper function. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=970a0122d81ff0a4400612cf80f7096780c452ff;p=TD_C.git TP 9 exo2: Add list_concat() linked list helper function. Signed-off-by: Jerome Benoit --- diff --git a/TP_9/exo2/clist.c b/TP_9/exo2/clist.c index 100f77d..4812ab8 100644 --- a/TP_9/exo2/clist.c +++ b/TP_9/exo2/clist.c @@ -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; diff --git a/TP_9/exo2/clist.h b/TP_9/exo2/clist.h index 5c3a68f..a08ec17 100644 --- a/TP_9/exo2/clist.h +++ b/TP_9/exo2/clist.h @@ -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_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); diff --git a/TP_9/exo2/exo2.c b/TP_9/exo2/exo2.c index 1b243af..09855d5 100644 --- a/TP_9/exo2/exo2.c +++ b/TP_9/exo2/exo2.c @@ -3,29 +3,40 @@ #include "clist.h" int main() { + link_t* head1 = NULL; + link_t* head2 = 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_clear(head1); + //list_clear(head2); list_clear(head); return 0;