C_TP_Forth/pileInt.c
2022-12-08 06:11:40 +01:00

49 lines
960 B
C

#include "pileInt.h"
#include <stdlib.h>
#include <stdio.h>
void initPileI( struct PileInt* p ) {
p->height = 0;
p->l = NULL;
}
void pushPileI( struct PileInt* p, programPointer i ) {
struct ListInt* aux = p->l;
p->l = (struct ListInt*)malloc(sizeof(struct ListInt));
if (p->l == NULL) {
fprintf(stderr, "Error: Could not allocate memory for the next cell on the stack");
return;
}
p->l->next = aux;
p->l->i = i;
++(p->height);
}
int topPileI( struct PileInt* p ) {
if ( p->height == 0 ) {
fprintf(stderr, "Error: The stack has no element, cannot find top");
exit(1);
}
return p->l->i;
}
void popPileI( struct PileInt* p ) {
struct ListInt* aux;
if ( p->height != 0 ) {
--(p->height);
aux = p->l;
p->l = p->l->next;
free(aux);
}
}
void endPileI( struct PileInt* p ) {
while (p->height != 0) {
popPileI(p);
}
}