APL/APL1.2/TP10/chainee_tables.c

47 lines
730 B
C
Raw Permalink Normal View History

2022-01-18 12:52:55 +01:00
#include <stdio.h>
#include <stdlib.h>
2022-01-18 15:45:21 +01:00
struct pile_ {
char* tab;
int size;
};
typedef struct pile_ pile;
2022-01-18 12:52:55 +01:00
void push(pile* p, char c) {
2022-01-18 15:45:21 +01:00
(p->tab)[p->size] = c;
p->size++;
p->tab = realloc(p->tab, (p->size)+1);
2022-01-18 12:52:55 +01:00
}
char pop(pile* p) {
2022-01-18 15:45:21 +01:00
char c = (p->tab)[p->size-1];
p->size--;
p->tab = realloc(p->tab, (p->size)+1);
2022-01-18 12:52:55 +01:00
return c;
}
int empty(pile p) {
2022-01-18 15:45:21 +01:00
return p.size == 0;
2022-01-18 12:52:55 +01:00
}
char top(pile p) {
2022-01-18 15:45:21 +01:00
return (p.tab)[(p.size) - 1];
2022-01-18 12:52:55 +01:00
}
void clear(pile* p) {
2022-01-18 15:45:21 +01:00
free(p->tab);
p->tab = calloc(1, sizeof(int));
p->size = 0;
2022-01-18 12:52:55 +01:00
}
pile create() {
2022-01-18 15:45:21 +01:00
pile* p = (calloc(1, sizeof(pile)));
p->tab = calloc(1, sizeof(char));
p->size = 0;
return *p;
2022-01-18 12:52:55 +01:00
}
pile destroy(pile* p) {
free(p);
}