diff --git a/ex2/main.c b/ex2/main.c index 2a68350eb3d3133a356f68da555cd56120e9332b..47450a338d54588704b273964a39eb7021542cff 100644 --- a/ex2/main.c +++ b/ex2/main.c @@ -1,6 +1,136 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +typedef struct _element { // Elément de liste + char data; + struct _element* next; +} element_t; + +void free_liste(element_t* liste1){ + element_t* prec = liste1; + element_t* current = liste1; + while(current->next != NULL){ + prec = current; + current = current->next; + free(prec); + } + free(current); +} + + +void add_liste(element_t* liste, char val){ + element_t* new = malloc(sizeof(element_t)); + new->data = val; + new->next = NULL; + element_t* current = liste; + while(current->next != NULL ){ + current = current->next; + } + current->next = new; +} + +void print_liste(element_t* liste){ + if(liste != NULL){ + element_t* current = liste; + while(current->next != NULL){ + printf("%c, ", current->data); + current = current->next; + } + printf("%d \n", current->data); + }else{ + printf("Empty liste \n"); + } +} int main(){ + +//////////// INIT VAL + char chaine = ' '; + element_t* liste1 = malloc(sizeof(element_t)); + element_t* liste2 = malloc(sizeof(element_t)); + + +/////////// GET INPUT + printf("Chaine 1 : "); + scanf("%hhd", &chaine); + + liste1->data = chaine; + liste1->next = NULL; + + while(chaine != '\n'){ + scanf("%c", &chaine); + add_liste(liste1, chaine); + } + + chaine = ' '; + printf("Chaine 2 : "); + scanf("%hhd", &chaine); + + liste2->data = chaine; + liste2->next = NULL; + + while(chaine != '\n'){ + scanf("%c", &chaine); + add_liste(liste2, chaine); + } + +////////// FUISON + element_t* fuse = malloc(sizeof(element_t)); + element_t* current1 = liste1->next; + element_t* current2 = liste2; + fuse->data = liste1->data; + fuse->next = NULL; + while(true){ + if(current1 == NULL && current2 == NULL){ + break; + }if(current1 == NULL){ + while(current2 != NULL){ + add_liste(fuse, current2->data); + current2 = current2->next; + } + break; + }if(current2 == NULL){ + while(current2 != NULL){ + add_liste(fuse, current1->data); + current1 = current1->next; + } + break; + } + add_liste(fuse, current1->data); + add_liste(fuse, current2->data); + current1 = current1->next; + current2 = current2->next; + + } + /*while(true){ + if(current1 == NULL && current2 == NULL){ + break; + }if(current1 == NULL && current2 != NULL){ + add_liste(fuse, current2->data); + current2 = current2->next; + }if(current1 != NULL && current2 == NULL){ + add_liste(fuse, current1->data); + current1 = current1->next; + }else{ + add_liste(fuse, current1->data); + add_liste(fuse, current2->data); + current2 = current2->next; + current1 = current1->next; + } + }*/ + + + + //print_liste(liste1); + //print_liste(liste2); + print_liste(fuse); + +/////////////// FREE MEMORY + free_liste(liste1); + free_liste(liste2); + free_liste(fuse); + return(0); }