-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvers2
More file actions
223 lines (160 loc) · 4.87 KB
/
vers2
File metadata and controls
223 lines (160 loc) · 4.87 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
//#include "IsE_Stack.h"
#define asserted(x) || !printf("[Error] on line number: %s\n" "return code: %s", __LINE__, x)
typedef double stackType;
const stackType poison = NAN;
const double ENLARGE_BIG_COEFFICIENT = 2;
const double ENLARGE_SMALL_COEFFICIENT = 1.5;
const double REDUCE_COEFFICIENT = 0.4;
struct Stack
{
size_t capacity;
size_t sz;
stackType data[0];
int errCode;
};
enum StackErrorCode
{
NO_STACK_ERRORS = 0,
BAD_SIZE = 1,
UNACCEPTABLE_Member = 2
};
enum FunctionErrorCode
{
NO_FUNCTION_ERRORS = 0,
NO_FREE_MEMORY = 1,
NULL_POINTER_IN_ARGUMENT = 2,
BAD_ARGUMENT = 3
};
int stackConstructor(struct Stack* thou, size_t capacity);
void stackDestructor(struct Stack* thou);
int pushIntoStack(struct Stack* thou, stackType newMember);
int enlargeStack(struct Stack* thou);
int reduceStack(struct Stack* thou);
stackType pop(struct Stack* thou);
stackType front(struct Stack* thou);
int stackError(struct Stack* thou);
int stackDump(struct Stack* thou);
int stackResize(struct Stack* thou, size_t newCapacity);
void fillStackWithPoison(struct Stack* thou, size_t beginInd, size_t endInd);
int main() {
return 0;
}
//--------------------------------------------------------
int stackConstructor(struct Stack* thou, size_t capacity)
{
assert(thou);
thou = (Stack*) calloc(1, sizeof(*thou) + sizeof(thou->data) * capacity) ;
if(thou asserted(NO_FREE_MEMORY))
return NO_FREE_MEMORY;
thou->capacity = capacity;
thou->sz = 0;
thou->errCode = NO_STACK_ERRORS;
for (size_t i = 0; i < capacity; i++)
(thou->data)[i] = poison;
return 0;
}
//------------------------------------------------------------
void stackDestructor(struct Stack* thou)
{
assert(thou);
free(thou);
thou = NULL;
}
//------------------------------------------------------------
int pushIntoStack(struct Stack* thou, stackType newMember)
{
assert(thou);
if (thou->sz == thou->capacity)
if(enlargeStack(thou) asserted(NO_FREE_MEMORY))
return NO_FREE_MEMORY;
(thou->data)[thou->sz++] = newMember;
return NO_FUNCTION_ERRORS;
}
//-------------------------------------------------------
int enlargeStack(struct Stack* thou)
{
if(!stackResize(thou, (size_t)(thou->capacity * ENLARGE_BIG_COEFFICIENT)))
return NO_FUNCTION_ERRORS;
if(!stackResize(thou, (size_t)(thou->capacity * ENLARGE_SMALL_COEFFICIENT)))
return NO_FUNCTION_ERRORS;
if(!stackResize(thou, thou->capacity + 100))
return NO_FUNCTION_ERRORS;
if(!stackResize(thou, thou->capacity + 1))
return NO_FUNCTION_ERRORS;
return NO_FREE_MEMORY;
}
//----------------------------------------------------------------
int reduceStack(struct Stack* thou)
{
if(!stackResize(thou, thou->capacity * REDUCE_COEFFICIENT))
return NO_FUNCTION_ERRORS;
return NO_FREE_MEMORY;
}
//----------------------------------------------------------------
stackType pop(struct Stack* thou)
{
if(int errNo = stackError(thou))
return poison;
if(thou->sz > 0){
stackType returnMember;
returnMember = (thou->data)[thou->sz--];
fillStackWithPoison(thou, thou->sz + 1, thou->sz + 1);
if (thou->sz <= thou->capacity * REDUCE_COEFFICIENT)
reduceStack(thou);
return returnMember;
}
else
return poison;
}
//-----------------------------------------------------------------------
int stackError(struct Stack* thou)
{
if(!thou)
return NULL_POINTER_IN_ARGUMENT;
if(thou->sz > thou->capacity || thou->sz * 3 < thou->capacity)
{
thou->errCode = BAD_SIZE;
return BAD_ARGUMENT;
}
if(thou->data[thou->sz] != poison)
{
thou->errCode = UNACCEPTABLE_Member;
return BAD_ARGUMENT;
}
return NO_STACK_ERRORS;
}
//-------------------------------------------------------------------------
int stackResize(struct Stack* thou, size_t newCapacity)
{
//size_t sz = thou->sz;
//int errcode = thou->errCode;
struct Stack* temp =(struct Stack*)realloc(thou, sizeof(*thou) + sizeof(thou->data) * newCapacity);
if(!temp)
return NO_FREE_MEMORY;
else
{
thou = temp;
thou->capacity = newCapacity;
return NO_FUNCTION_ERRORS;
}
}
//-----------------------------------------------------------------------------
void fillStackWithPoison(struct Stack* thou, size_t beginInd, size_t endInd)
{
for(size_t ind = beginInd; ind <= endInd; ++ind)
(thou->data)[ind] = poison;
}
//--------------------------------------------------------------------------
int stackDump(struct Stack* thou, char* problem)
{
if(!thou)
{
printf("NULL POINTER: THERE IS NO STACK");
return NULL_POINTER_IN_ARGUMENT;
}
printf("StackT");
}