VaultBy ENSAMIENParrainage
C151 lines

03-liste-chainee-operations.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Exercice : opérations sur une liste chaînée
 * Construire une liste aléatoire, insérer au début et à la fin, afficher à l'envers
 * (récursif), supprimer en tête, en fin, par valeur, puis libérer la liste.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct cell {
    int val;
    char s[2];
    struct cell* next;
} cell;

cell* build_n_random(int n) {
    if (n <= 0) return NULL;
    cell* head = NULL, *current = NULL;
    for (int i = 0; i < n; i++) {
        cell* new_cell = malloc(sizeof(cell));
        new_cell->val = rand() % 26;
        new_cell->s[0] = 'A' + new_cell->val;
        new_cell->s[1] = '\0';
        new_cell->next = NULL;
        if (!head) head = new_cell;
        else current->next = new_cell;
        current = new_cell;
    }
    return head;
}

void print_list(const cell* head) {
    while (head) {
        printf("[%d, %s] -> ", head->val, head->s);
        head = head->next;
    }
    printf("NULL\n");
}


void push_front(cell** phead, int v, char c) {
    cell* new_cell = malloc(sizeof(cell));
    new_cell->val = v;
    new_cell->s[0] = c;
    new_cell->s[1] = '\0';
    new_cell->next = *phead;
    *phead = new_cell;
}

void push_back(cell** phead, int v, char c) {
    cell* new_cell = malloc(sizeof(cell));
    new_cell->val = v;
    new_cell->s[0] = c;
    new_cell->s[1] = '\0';
    new_cell->next = NULL;
    if (*phead == NULL) {
        *phead = new_cell;
        return;
    }
    cell* cur = *phead;
    while (cur->next != NULL) cur = cur->next;
    cur->next = new_cell;
}

void print_reverse_rec(const cell* head) {
    if (head == NULL) return;
    print_reverse_rec(head->next);
    printf("[%d, %s] -> ", head->val, head->s);
}

void pop_front(cell** phead) {
    if (*phead == NULL) return;
    cell* tmp = *phead;
    *phead = (*phead)->next;
    free(tmp);
}

void pop_back(cell** phead) {
    if (*phead == NULL) return;
    if ((*phead)->next == NULL) {
        free(*phead);
        *phead = NULL;
        return;
    }
    cell* cur = *phead;
    while (cur->next->next != NULL) cur = cur->next;
    free(cur->next);
    cur->next = NULL;
}

void remove_first_by_val(cell** phead, int v) {
    if (*phead == NULL) return;
    cell* cur = *phead, *prev = NULL;
    while (cur && cur->val != v) {
        prev = cur;
        cur = cur->next;
    }
    if (cur == NULL) return;
    if (prev == NULL) *phead = cur->next;
    else prev->next = cur->next;
    free(cur);
}

void free_list(cell** phead) {
    cell* cur = *phead;
    while (cur) {
        cell* tmp = cur;
        cur = cur->next;
        free(tmp);
    }
    *phead = NULL;
}

int main() {
    srand(time(NULL));

    cell* list = build_n_random(5);
    printf("Liste initiale :\n");
    print_list(list);

    printf("\nInsertion au debut:\n");
    push_front(&list, 0, 'A');
    print_list(list);

    printf("\nInsertion en fin:\n");
    push_back(&list, 25, 'Z');
    print_list(list);

    printf("\nreverse:\n");
    print_reverse_rec(list);
    printf("NULL\n");

    printf("\nSuppression du premier elt :\n");
    pop_front(&list);
    print_list(list);

    printf("\nSuppression du dernier elt :\n");
    pop_back(&list);
    print_list(list);

    printf("\nSuppression du premier elt de val=10 :\n");
    remove_first_by_val(&list, 10);
    print_list(list);

    printf("\nSuppression de la liste :\n");
    free_list(&list);
    print_list(list);

    return 0;
}

    Type at least 2 letters. ↑ ↓ to move, Enter to open.