-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.c
44 lines (35 loc) · 820 Bytes
/
stack.c
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
#include "stack.h"
void stackInit(stack *s, int capacity) {
s->p = (WORD *) malloc(sizeof(WORD) * capacity);
s->capacity = capacity;
s->length = 0;
}
void stackClear(stack *s) {
s->length = 0;
}
void stackFree(stack *s) {
free(s->p);
}
void stackPush(stack *s, WORD elem) {
if (s->length >= s->capacity) {
fputs("stack overflow.\n", stderr);
quit(EXIT_FAILURE);
}
*(s->p + s->length) = elem;
s->length++;
}
WORD stackPop(stack *s) {
if (s->length <= 0) {
fputs("stack overflow.\n", stderr);
quit(EXIT_FAILURE);
}
s->length--;
return *(s->p + s->length);
}
WORD stackTop(stack *s) {
if (s->length <= 0) {
fputs("stack overflow.\n", stderr);
finalize(EXIT_FAILURE);
}
return *(s->p + s->length - 1);
}