-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
49 lines (43 loc) · 826 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
46
47
48
49
#include"stack.h"
void Stack_Init(Stack *S)
{
S->size = 0;
int i;
for(i = 0; i < STACK_MAX; i++) {
S->data[i] = EMPTY;
}
}
int Stack_Top(Stack *S)
{
if (S->size == 0) {
/* can't be used with ncurses
fprintf(stderr, "Error: stack empty\n");
*/
return EMPTY;
}
return S->data[S->size-1];
}
void Stack_Push(Stack *S, int c)
{
if (S->size < STACK_MAX)
S->data[S->size++] = c;
/*
else
can't be used with ncurses
fprintf(stderr, "Error: stack full\n");
*/
}
int Stack_Pop(Stack *S)
{
if (S->size == 0) {
/* can't be used with ncurses
fprintf(stderr, "Error: stack empty\n");
*/
return -1;
}
else {
int top = Stack_Top(S);
S->data[--S->size] = EMPTY;
return top;
}
}