#ifndef PILE_H #define PILE_H 1 struct pile ; struct pile* nouvelle(); void detruire(struct pile*); int est_vide(struct pile*); int empiler(struct pile*, int); int depiler(struct pile*); void afficher(struct pile*); #endif // ! PILE_H
#include<stdio.h> #include"pile.h" struct pile* la_pile; int main(int argc, char** argv) { int encore = 1; la_pile = nouvelle(); if (la_pile == NULL) return 1; do { int ch = getchar(); int val; switch(ch) { case 'q': encore = 0; break; case 'a': afficher(la_pile); break; case 'e': if ( scanf("%d", &val) != 1 || empiler(la_pile, val) ) fputs("Ne peut empiler", stderr); break; case 'd': val = depiler(la_pile); printf("-> %d\n", val); break; case 'v': if (est_vide(la_pile)) puts("vide"); else puts("non-vide"); break; default: fprintf(stderr, "Commande inconnue: %c\n", ch); } } while (encore); detruire(la_pile); return 0; }
#include<stdio.h> #include<stdlib.h> #include"pile.h" struct __pile { int val; struct __pile* next; }; struct pile { struct __pile *p; }; struct pile* nouvelle() { struct pile* p; p = malloc(sizeof(*p)); if (p == NULL) return NULL; p->p = NULL; return p; } void __detruire(struct __pile* p) { while (p != NULL) { struct __pile* aux = p->next; free(p); p = aux; } } void detruire(struct pile* p) { __detruire(p->p); free(p); } int est_vide(struct pile* p) { return (p->p == NULL); } int empiler(struct pile* p, int val) { struct __pile* n; n = malloc(sizeof(*n)); if (n == NULL) return 1; n->val = val; n->next = p->p; p->p = n; return 0; } int depiler(struct pile* p) { struct __pile* n = p->p; int val = n->val; p->p = n->next; free(n); return val; } void afficher(struct pile* p) { struct __pile* n; printf("<"); for (n = p->p; n != NULL; n = n->next) { printf("%d; ", n->val); } printf(">\n"); }
#include"pile.h" #include<stdlib.h> #include<stdio.h> #define TAILLE 4 struct pile { int data[TAILLE]; int* top; }; struct pile* nouvelle() { struct pile* p; p = malloc(sizeof(*p)); if (p == NULL) return NULL; p->top = p->data; return p; } void detruire(struct pile* p) { free(p); } int est_vide(struct pile* p) { return (p->top == p->data); } int empiler(struct pile* p, int val) { if (p->top - p->data >= TAILLE) return 1; *(p->top) = val; ++(p->top); return 0; } int depiler(struct pile* p) { --(p->top); return *(p->top); } void afficher(struct pile* p) { int *i = p->top; printf("<"); while (i != p->data) { --i; printf("%d; ", *i); } printf(">\n"); }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionmais j ai chercher et j ai ecris le code de la pile,mais comment avoir l interface que j ai jamais entendu parlé c pourquoi!!!