forked from czertyaka/prosoft-c-stack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcstack.c
More file actions
113 lines (95 loc) · 2.73 KB
/
cstack.c
File metadata and controls
113 lines (95 loc) · 2.73 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
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
#include "cstack.h"
#include <stddef.h>
#include <stdlib.h>
typedef struct item
{
unsigned int size;
char* data;
struct item *prev;
} item;
typedef struct stack_list
{
int size;
int capacity;
item** stack;
} stack_list_;
stack_list_ stack_list = {.size = 0, .capacity = 10, .stack = NULL};
static int is_initialized = 0;//за такое надо банить
static void stack_list_init() {
if (!is_initialized){
stack_list.stack = (item**)calloc(stack_list.capacity,sizeof(item*));
is_initialized =1;
}
}
hstack_t stack_new(void)
{
if (stack_list.size >= stack_list.capacity) {
stack_list.capacity *= 2;
stack_list.stack = (item**)realloc(stack_list.stack, stack_list.capacity * sizeof(item*));
if (!stack_list.stack) return -1;
}
stack_list.stack[stack_list.size] = NULL;
return stack_list.size++;
}
void stack_free(const hstack_t hstack)
{
if (stack_valid_handler(hstack)) {
item* current = stack_list.stack[hstack];
while (current) {
item* prev = current->prev;
free(current->data);
free(current);
current = prev;
}
stack_list.stack[hstack] = NULL;
}
}
int stack_valid_handler(const hstack_t hstack)
{
return (hstack >= 0 && hstack < stack_list.size
&& stack_list.stack[hstack] != NULL);
}
unsigned int stack_size(const hstack_t hstack)
{
if (stack_valid_handler(hstack)) {
unsigned int size = 0;
item* current = stack_list.stack[hstack];
while (current) {
size++;
current = current->prev;
}
return size;
}
return 0;
}
void stack_push(const hstack_t hstack, const void* data_in, const unsigned int size)
{
if (!data_in || size == 0 || !stack_valid_handler(hstack))//проверка входных данных и хендлер
return;
item* new_item = (item*)malloc(sizeof(item));
if (!new_item) return;
new_item->data = (char*)malloc(size);
if (!new_item->data) {
free(new_item);
return;
}
for (unsigned int i = 0; i < size; i++) {
new_item->data[i] = ((char*)data_in)[i];
}
new_item->size = size;
new_item->prev = stack_list.stack[hstack];
stack_list.stack[hstack] = new_item;
}
unsigned int stack_pop(const hstack_t hstack, void* data_out, const unsigned int size)
{
if (!data_out || size == 0 || !stack_valid_handler(hstack)) return 0;
item* top = stack_list.stack[hstack];
if (!top || top->size != size) return 0;
for (unsigned int i = 0; i < size; i++) {
((char*)data_out)[i] = top->data[i];
}
stack_list.stack[hstack] = top->prev;
free(top->data);
free(top);
return size;
}