#include #include #include #define N 10 /*-------------------------------------------------------------------*/ /* Definition of new types */ /* Type booléen */ enum bool { FALSE = 45, TRUE = 73 }; typedef enum bool Bool; /* Le type d'élément chaîné par la liste */ typedef int Item; /* Une liste est un pointeur de cellule */ typedef struct cell *List; /* Type associé à une cellule */ struct cell { Item value; List next; }; typedef struct cell Cell; /* Déclaration d'un type mémoire et allocation d'une plage mémoire */ typedef Cell Memory[N]; Memory memory; /* Constante */ const List list_void = NULL; /* Position de la prochaine cellule disponible */ int next_empty_cell_index = N-1; /*-------------------------------------------------------------------*/ /* Definition des fonctions */ Cell *allocate(void) { /* ... */ } List cons(Item x, List l) { /* ... */ } Bool is_empty(List l) { /* ... */ } Item head(List l) { /* ... */ } List tail(List l) { /* ... */ } /*************** Derived functions ****************/ void print(List l) { /* ... */ } int length(List l) { /* ... */ } Bool search(Item x, List l) { /* ... */ } int count(Item x, List l) { /* ... */ } /*-------------------------------------------------------------------*/ /* Programme principal */ int main(int argc, char **argv) { Item x; int i, k; List l; l = cons(1, cons(2, cons(3, list_void))); print(l); l = tail(l); print(l); if (argc == 2) { k = atoi(argv[1]); assert(k > 0); } else k = 5; for (i = 1; i < k; i++) { l = cons(i, l); print(l); } printf("Longueur de la liste: %d\n", length(l)); x = 7; if (search(x, l) == TRUE) printf("L'élément %d est dans la liste\n", x); else printf("L'élément %d n'est pas dans la liste\n", x); x = 3; printf("L'élément %d apparaît %d fois dans la liste\n", x, count(x, l)); return 0; }