-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
122 lines (83 loc) · 1.5 KB
/
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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
typedef struct stack {
int sp;
int stack_size;
int *array;
int max_sp;
} stack_t;
int stack_get_sp(stack_t *s)
{
return s->sp;
}
void stack_set_sp(stack_t *s, int val)
{
s->sp = val;
}
static int stack_get_array_val(stack_t *s, int sp)
{
return s->array[sp];
}
static int stack_get_stack_size(stack_t *s)
{
return s->stack_size;
}
static int stack_set_stack_size(stack_t *s, int size)
{
s->stack_size = size;
}
static void stack_set_array_val(stack_t *s, int sp, int val)
{
s->array[sp] = val;
}
void stack_set_max_sp(stack_t *s, int count)
{
s->max_sp = count;
}
int stack_get_max_sp(stack_t *s)
{
return s->max_sp;
}
int stack_pop(stack_t *s)
{
int sp = stack_get_sp(s);
if (sp == 0) {
/*TODO handle error.*/
fprintf(stderr, "failed to pop value\n");
}
stack_set_sp(s, sp - 1);
return stack_get_array_val(s, sp);
}
int stack_push(stack_t *s, int val)
{
int sp = stack_get_sp(s) + 1;
if (sp > stack_get_stack_size(s)) {
fprintf(stderr, "failed to push value\n");
return 1;
}
stack_set_sp(s, sp);
stack_set_array_val(s, sp, val);
return 0;
}
int stack_init(stack_t **s)
{
*s = malloc(sizeof(stack_t));
if (!(*s))
goto error;
(*s)->array = malloc(sizeof(int) * STACK_SIZE);
if (!(*s)->array)
goto error;
stack_set_sp(*s, 0);
stack_set_max_sp(*s, 0);
stack_set_stack_size(*s, STACK_SIZE);
return 0;
error:
perror("malloc");
return 1;
}
void stack_free(stack_t *s)
{
free(s->array);
free(s);
}