-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack_array.c
More file actions
59 lines (50 loc) · 1.49 KB
/
Copy pathstack_array.c
File metadata and controls
59 lines (50 loc) · 1.49 KB
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
//
// Created by HomorSmith on 2017/5/10.
//
#include "stack_array.h"
void init_stack_array(PSTACK_ARRAY *pstack_array) {
*pstack_array = malloc(sizeof(STACK_ARRAY));
if ((*pstack_array) == NULL) {
exit(OVERFLOW);
}
(*pstack_array)->stack = malloc(sizeof(ELEMENT) * MAX_STACK_ARRAY_SIZE);
(*pstack_array)->top = -1;
(*pstack_array)->size = MAX_STACK_ARRAY_SIZE;
};
void push_stack_array(PSTACK_ARRAY pstack_array, ELEMENT *data) {
if (is_full_stack_array(pstack_array)) {
printf("array stack is full");
return;
}
pstack_array->top++;
pstack_array->stack[pstack_array->top] = *data;
};
void pop_stack_array(PSTACK_ARRAY pstack_array, ELEMENT *data) {
if (is_empty_stack_array(pstack_array)) {
printf("array stack is empty");
return;
}
*data = pstack_array->stack[pstack_array->top];
pstack_array->top--;
};
bool is_empty_stack_array(PSTACK_ARRAY pstack_array) {
if (pstack_array->top == -1) {
return true;
}
return false;
};
bool is_full_stack_array(PSTACK_ARRAY pstack_array) {
return pstack_array->top == MAX_STACK_ARRAY_SIZE;
};
void print_stack_array(PSTACK_ARRAY pstack_array) {
while (!is_empty_stack_array(pstack_array)) {
int a;
pop_stack_array(pstack_array, &a);
printf("%d\n", a);
}
};
void destroy_stack_array(PSTACK_ARRAY *pstack_array) {
free((*pstack_array));
free(((*pstack_array)->stack));
printf("has been destroyed stack array");
}