/************************************** * stack.c * * Programme d'exemple implémentant un stack comme structure * chaînée * **************************************/ #include #include typedef struct fraction_t { int num; int den; } fraction; ///AAA typedef struct node_t { struct fraction_t *data; struct node_t *next; } node; struct node_t *stack; // sommet de la pile // ajoute un élément au sommet de la pile void push(struct fraction_t *f) { struct node_t *n; n=(struct node_t *)malloc(sizeof(struct node_t)); if(n==NULL) exit(EXIT_FAILURE); n->data = f; n->next = stack; stack = n; } // retire l'élément au sommet de la pile struct fraction_t * pop() { if(stack==NULL) return NULL; // else struct fraction_t *r; struct node_t *removed=stack; r=stack->data; stack=stack->next; free(removed); return (r); } ///BBB // affiche le contenu de la pile void display() { struct node_t *t; t = stack; while(t!=NULL) { if(t->data!=NULL) { printf("Item at addr %p : Fraction %d/%d Next %p\n",t,t->data->num,t->data->den,t->next); } else { printf("Bas du stack %p\n",t); } t=t->next; } } // exemple int main(int argc, char *argv[]) { struct fraction_t demi={1,2}; struct fraction_t tiers={1,3}; struct fraction_t quart={1,4}; struct fraction_t zero={0,1}; // initialisation stack = (struct node_t *)malloc(sizeof(struct node_t)); stack->next=NULL; stack->data=NULL; display(); push(&zero); display(); push(&demi); push(&tiers); push(&quart); display(); struct fraction_t *f=pop(); if(f!=NULL) printf("Popped : %d/%d\n",f->num,f->den); return(EXIT_SUCCESS); } ///CCC