53 lines
853 B
C
53 lines
853 B
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
struct pile{
|
||
|
char val;
|
||
|
struct pile *suiv;
|
||
|
};
|
||
|
|
||
|
typedef struct pile pile;
|
||
|
|
||
|
int empty (pile *p){
|
||
|
return p->suiv==NULL;
|
||
|
}
|
||
|
|
||
|
void push (pile **p,char v){
|
||
|
pile *nouvelle=malloc(sizeof(pile));
|
||
|
nouvelle->val=v;
|
||
|
nouvelle->suiv=*p;
|
||
|
}
|
||
|
|
||
|
char pop (pile **p){
|
||
|
pile *temp=malloc(sizeof(pile));
|
||
|
temp=*p;
|
||
|
(*p)->val=(*p)->suiv->val;
|
||
|
(*p)->suiv=(*p)->suiv->suiv;
|
||
|
return temp->val;
|
||
|
free(temp);
|
||
|
}
|
||
|
|
||
|
int main(){
|
||
|
char c=0;
|
||
|
pile *p=NULL;
|
||
|
|
||
|
printf("La pile attend vos ordres\n");
|
||
|
while (c!='q'){
|
||
|
scanf("%c",&c);
|
||
|
if (c=='+'){
|
||
|
scanf("%c",&c);
|
||
|
push((&p),c);
|
||
|
printf("Le caractère %c a été ajouté\n",c);
|
||
|
}else{
|
||
|
if(c=='-'){
|
||
|
if (empty(p)==1){
|
||
|
printf("La pile est vide !\n");
|
||
|
}else{
|
||
|
printf("Le caractère %c a été supprimé\n",pop(&p));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
printf("Au revoir\n");
|
||
|
return 0;
|
||
|
}
|