#include #include #include #include struct cyclic_t{ int data; struct cyclic_t* next; }; typedef struct cyclic_t lc; //Cherche si la clé k est présente dans // la liste l //Renvoi vrai si la clé est présente // faux sinon bool cyclic_pres(int k, lc* l); //Ajoute la clé k à la liste cyclique // pointée par l //Si l==NULL, alors une nouvelle liste // cyclique est crée //Renvoi vrai si l'ajout a été effectué // faux sinon bool cyclic_ajout(int k, lc** l); //Retire la cellule contenant la clé k // dans la liste pointée par l //Si la première cellule est modifiée, ce // changement est répercuté sur l //Renvoi vrai si la cellule a été retirée // faux sinon bool cyclic_retire(int k, lc** l); bool cyclic_pres(int k, lc* l){ lc* tmp = l; if(l == NULL){ return false; }else if (tmp->data == k){ return true; } while(tmp->next != l){ tmp = tmp->next; if(tmp->data == k) return true; } return false; } bool cyclic_ajout(int k, lc** l){ if(*l == NULL){ lc* tmp = (lc*)malloc(sizeof(lc)); if(tmp == NULL) return false; tmp->data = k; tmp->next = *l == NULL ? tmp : *l; *l = tmp; }else{ lc* tmp = (lc*)malloc(sizeof(lc)); if(tmp == NULL){ return false; } tmp->data = k; tmp->next = (*l)->next; (*l)->next = tmp; } return true; } bool cyclic_retire(int k, lc** l){ if(*l == NULL || !cyclic_pres(k,*l)) return false; lc* tmp = *l; while(tmp->next->data != k){ tmp = tmp->next; } if(tmp->next == tmp){ free(tmp); *l = NULL; }else{ lc* m = tmp->next; tmp->next = m->next; if(m == *l){ *l = tmp->next; } free(m); } return true; } int main(int argc, char** argv){ lc* l = NULL; assert(cyclic_ajout(1,&l)); assert(cyclic_pres(1,l)); assert(!cyclic_pres(2,l)); assert(cyclic_retire(1,&l)); assert(l == NULL); for(int i =0; i<10; i++){ assert(cyclic_ajout(i,&l)); } for(int i =0; i<10; i++){ assert(cyclic_pres(i,l)); } for(int i=0; i<10; i++){ assert(cyclic_retire(i,&l)); for(int j=0; j<=i; j++){ assert(!cyclic_pres(j,l)); } for(int j=i+1; j<10; j++){ assert(cyclic_pres(j,l)); } } assert(l == NULL); return EXIT_SUCCESS; argc = argc; argv = argv; }