1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| typedef int Datatype;
typedef struct Sequent_stack { Datatype *stack; int size; int top; }seqStack;
seqStack *init_stack(int size) { seqStack *s = malloc(sizeof(seqStack)); if(s != NULL) { s->stack = malloc(sizeof(Datatype) * size); if(s->stack == NULL) { free(s); return NULL; } s->size = size; s->top = -1; } return s; }
bool is_full(seqStack *s) { return s->top == s->size-1; }
bool is_empty(seqStack *s) { return s->top == -1; }
bool push(seqStack *s, Datatype data) { if(is_full(s)) return false; s->top++; s->stack[s->top] = data; return true; }
bool pop(seqStack *s, Datatype *data) { if(is_empty(s)) return false; *data = s->stack[s->top]; s->top--; return true; }
bool top(seqStack *s, Datatype *data) { if(is_empty(s)) return false; *data = s->stack[s->top]; return true; }
|