4 Janvier
This commit is contained in:
parent
a48ec5a967
commit
2da55ee91e
53
DEV1.1/TP17:Pile/chainee.c
Normal file
53
DEV1.1/TP17:Pile/chainee.c
Normal file
@ -0,0 +1,53 @@
|
||||
#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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user