-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
106 lines (90 loc) · 1.83 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
#include "share.h"
#include "stack.h"
stack *stack_create(int size, stack_type *st) {
stack *s = NULL;
int ssize;
ssize = size < STACK_DEFAULT_SIZE ? STACK_DEFAULT_SIZE : size;
s = (stack *)calloc(sizeof(stack), 1);
if (s == NULL) {
return NULL;
}
s->ele = (void **)calloc(sizeof(void *), ssize);
if (s->ele == NULL) {
free(s);
return NULL;
}
s->size = ssize;
s->top = 0;
s->st = st;
return s;
}
void stack_release(stack *s) {
int i = 0;
int used = s->top;
if (s->st && s->st->free) {
for (i = 0; i < used; i++) {
s->st->free(s->ele[i]);
}
}
if (s) {
free(s->ele);
free(s);
}
return;
}
/*
bool stack_full(stack *s) {
if (s->top == s->size) {
return true;
}
return false;
}
bool stack_empty(stack *s) {
if (s->top == 0) {
return true;
}
return false;
}
*/
bool stack_push(stack *s, void *data) {
int size;
void **ptr;
if (stack_full(s)) {
size = s->size * 2;
ptr = (void **)realloc(s->ele, size * sizeof(void *));
if (ptr == NULL) {
return false;
}
s->ele = ptr;
s->size = size;
}
s->ele[s->top++] = data;
return true;
}
void *stack_pop(stack *s) {
if (stack_empty(s)) {
return NULL;
}
return s->ele[--s->top];
}
void *stack_top(stack *s) {
if (stack_empty(s)) {
return NULL;
}
return s->ele[s->top - 1];
}
#ifdef STACK_TEST
#include <stdio.h>
#include <assert.h>
int main() {
stack *s = stack_create(8, NULL);
assert(s != NULL);
int a = 10;
int b = 20;
stack_push(s, &a);
stack_push(s, &b);
printf("top: %d\n", *(int *)stack_pop(s));
printf("top: %d\n", *(int *)stack_pop(s));
stack_release(s);
}
#endif