Skip to content
Snippets Groups Projects
Select Git revision
  • f0bea494e703ed780f719f2231e02cc6cbb9c8c1
  • main default protected
2 results

ex1.c

Blame
  • user avatar
    leo.harb authored
    f0bea494
    History
    ex1.c 3.45 KiB
    // lien du git : https://githepia.hesge.ch/leo.harb/examen_programmation_2023
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    typedef struct element_t
    {
        int value;
        struct element_t *next;
    } element;
    
    typedef struct ll_t
    {
        element *head;
        element *pos;
    }ll;
    
    ll ll_create(){
        ll res;
        res.head = NULL;
        res.pos = NULL;
        return res;
    }
    
    void ll_destroy(ll *list){
        list->pos = list->head;
        while (list->pos != NULL)
        {
            element *tmp = list->pos->next;
            free(list->pos);
            list->pos = tmp;
        }
        list->head = NULL;
        list->pos = NULL;
    }
    
    bool ll_is_empty(ll list){
        return list.head == NULL;
    }
    
    // Est-ce que pos est le 1er élément?
    bool ll_is_head(ll list){
        return list.pos == list.head;
    }
    
    // Est-ce que pos est le dernier élément?
    bool ll_is_tail(ll list){
        return list.pos->next == NULL;
    }
    
    // data est-elle dans la liste?
    bool ll_is_present(ll list, float data){
        while (list.pos->next != NULL)
        {
            if(list.pos->value == data){
                return true;
            }
            list.pos = list.pos->next;
        }
        return false;
    }
    
    void ll_push(ll *list, int data){
        element *el = (element*)malloc(sizeof(element));
        el->value = data;
        el->next = list->head;
        list->head = el;
        list->pos = el;
    }
    
    void ll_insert(ll *list, int data){
        element *el = (element*)malloc(sizeof(element));
        if(list->pos == list->head){
            list->head = el;
        }
        el->next = list->pos;
        element *tmp = list->head;
        while (tmp->next!=list->pos);
        {
            tmp = tmp->next;
        }
        tmp->next = el;
    }
    
    // affiche la liste
    void ll_print(ll *list){
        while (list->pos != NULL)
        {
            printf("%f ", list->pos->value);
            list->pos = list->pos->next;
        }
    }
    
    bool ll_contains(ll *list, int data){
        element* tmp = list->head;
        while (tmp != NULL)
        {
            if(tmp->value == data){
                return true;
            }
        }
        return false;
    }
    
    void ll_sort_in(ll *list1, ll *list2, ll *res){
        list1->pos = list1->head;
        res->pos = res->head;
        while (list1->pos != NULL)
        {
            if(!ll_contains(res, list1->pos->value)){
                while (res->pos->value <= list1->pos->value)
                {
                    res->pos = res->pos->next;
                }
                ll_insert(res, list1->pos->value);
            }
            list1->pos = list1->pos->next;
        }
        while (list2->pos != NULL)
        {
            if(!ll_contains(res, list2->pos->value)){
                while (res->pos->value <= list2->pos->value)
                {
                    res->pos = res->pos->next;
                }
                ll_insert(res, list2->pos->value);
            }
            list2->pos = list2->pos->next;
        }
    }
    
    int main(){
        int len1 = 0;
        int len2 = 0;
        float values1[256];
        float values2[256];
        int left = 0;
        char string1[256];
        char string2[256];
    
        printf("Entrez la liste 1:");
        scanf("%[^\n]%*c", string1);
        
        float temp1;
        char *p1 = string1;
        int cpt1 = 0;
        while(sscanf(p, "%f%n", &temp1, &left) == 1) {
            values1[cpt1] = temp1;
            p += left;
            cpt1++;
        }
    
        printf("Entrez la liste 2:");
        scanf("%[^\n]%*c", string1);
        float temp2;
        char *p2 = string2;
        int cpt2 = 0;
        while(sscanf(p, "%f%n", &temp2, &left) == 1) {
            values1[cpt1] = temp2;
            p += left;
            cpt1++;
        }
    
        
        ll list1 = ll_create();
        ll list2 = ll_create();
        ll res = ll_create();
        
    
        dll_print(&list);
        dll_destroy(&list);
        return 0;
    }