-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.h
More file actions
138 lines (117 loc) · 3.3 KB
/
Copy pathStack.h
File metadata and controls
138 lines (117 loc) · 3.3 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// Created by plyus_000 on 30.10.2015.
//
#ifndef STACK_CPU_STACK_H
#define STACK_CPU_STACK_H
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
typedef struct stack
{
int* data;
long count, maxCount;
} Stack;
const int START_SIZE = 128;
void Stack_Dump(Stack* This, char* name);
#define ASSERT_OK(type, what) \
if(!type##_OK(what)) \
{ \
type##_Dump(what, #what); \
printf(#type " failed in %s, %d", __FILE__, __LINE__); \
abort(); \
}
int Stack_OK(Stack* This)
{
if (!This) return 0;
if (This->count < 0 || This->count > This->maxCount || This->data == NULL || This->maxCount < 0)
return 0;
else return 1;
}
void Stack_Dump(Stack* This, const char name[])
{
printf("%s = Stack (%s)\n",name, Stack_OK(This)? "ok" : "CORRUPTED!!!");
printf("\t{\n");
if (This)
{
printf("\tcurrent size = %ld\n", This->count);
printf("\tmax size = %ld\n", This->maxCount);
if (This->count == 0)
{
if (This->data)
printf("\tdata is empty\n");
else printf("\tdata array is corrupted (NULL pointer)\n");
}
else
{
if (!This->data)
printf("\tdata array is corrupted (NULL pointer)\n");
else
{
if (This->count > 0)
{
printf("\tdata = \n");
for (long i = 0; i < This->count; i++)
printf("\t\t[%ld] %ld\n", i, (This->data)[i]);
}
else
{
printf("\tdata is probably empty. Lets see the first elements:\n");
for(long i = 0; i < 10; i++)
printf("\t\t[%ld] %ld\n", i, (This->data)[i]);
}
}
}
}
else
printf("\tStack is NULL\n");
printf("\t}\n");
}
#define Stack_dump(name) Stack_Dump((name), #name);
void Stack_ctor(Stack* This)
{
assert(This);
This->count = 0;
This->maxCount = START_SIZE;
This->data = (int*)calloc(This->maxCount, sizeof((This->data)[1]));
ASSERT_OK(Stack, This);
}
void Stack_dtor(Stack* This)
{
ASSERT_OK(Stack, This);
free(This->data);
This->data = NULL;
This->count = -1;
This->maxCount = -1;
}
int push_st(Stack* st, int val)
{
ASSERT_OK(Stack, st);
if (st->count == st->maxCount)
{
int *temp = (int*)realloc(st->data, (st->maxCount * 2)*sizeof((st->data)[0]));
if (temp == NULL || errno != 0)
return 0;
st->data = temp;
st->maxCount *= 2;
}
(st->data)[st->count++] = val;
ASSERT_OK(Stack, st);
return 1;
}
/// Если элементов в стеке нет, то возвращает 0, ставит errno = EACCESS
int pop_st(Stack* st)
{
ASSERT_OK(Stack, st);
if (st->count > 0)
{
st->count--;
return (st->data)[st->count];
}
else
{
errno = EACCES;
return 0;
}
}
#endif //STACK_CPU_STACK_H