-
Notifications
You must be signed in to change notification settings - Fork 9
/
stack.c
45 lines (34 loc) · 774 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
45
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "stack.h"
#include "main.h"
stack_t STACK;
// returns zero when stack is empty
int stack_not_empty(stack_t *ps) {
return (ps->size);
}
void dump_stack(stack_t *ps) {
while(ps->size != 0) {
printf("%d %s\n",(int)ps->size,pop(ps));
}
}
void push(stack_t *ps, char *buffer) {
if (ps->size == STACKSIZE) {
fputs("Error: stack overflow!!!\n", stderr);
dump_stack(ps);
abort();
} else {
//ps->items[ps->size++] = x;
strcpy(ps->items[ps->size++],buffer);
}
}
char * pop(stack_t *ps) {
if (ps->size == 0){
fputs("Error: stack underflow!!!\n", stderr);
abort();
} else {
return ps->items[--ps->size];
}
}