ProjetGIT_TP_Forth/PILE/pile.c
2020-10-01 16:13:13 +02:00

53 lines
1.1 KiB
C

#include "pile.h"
#include <stdlib.h>
#include <stdio.h>
int pop(struct pile * p)
{
int res;
if (p->deb == NULL)
{
printf("Rien à depr, renvoie 0\n");
res = 0;
}
else
{
struct cell * aux = p->deb;
res = p->deb->val;
p->deb = p->deb->suiv;
p->taille--;
free(aux);
}
return res;
}
void Affichage(struct pile *p)
{
struct cell *aux = p->deb;
while (aux != NULL)
{
printf("%d -> ", (aux->val));
aux = aux->suiv;
}
printf("NULL\n");
}
void init(struct pile ** une_pile) {
*une_pile = (struct pile *)malloc(sizeof(struct pile));
}
void push(int e, struct pile * une_pile) {
struct cell * aux;
if (une_pile->taille == 0) {
une_pile->taille = 1;
une_pile->deb = (struct cell *)malloc(sizeof(struct cell));
une_pile->deb->suiv = NULL;
une_pile->deb->val = e;
} else {
une_pile->taille += 1;
aux = une_pile->deb;
une_pile->deb = (struct cell *)malloc(sizeof(struct cell));
une_pile->deb->val = e;
une_pile->deb->suiv = aux;
}
}