Сообщение было отмечено Doku как решение
Решение
stack.h : C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| #ifndef STACK_H
#define STACK_H
typedef struct TStack* STACK_HANDLE;
enum
{
stack_no_error,
stack_error_mem_alloc,
stack_error_invalid_handle,
stack_error_invalid_argument,
stack_error_empty_stack
};
int StackCreate(STACK_HANDLE* stackHandle, const size_t stackElementSize);
void StackDelete(STACK_HANDLE* stackHandle);
int IsStackEmpty(STACK_HANDLE stackHandle);
int StackPush(STACK_HANDLE stackHandle, const void* const elementAddress);
int StackPop(STACK_HANDLE stackHandle, void* const elementAddress);
int StackCopy(STACK_HANDLE destinationStackHandle, STACK_HANDLE sourceStackHandle);
#endif |
|
stack.c : 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
| #include <stdlib.h>
#include <string.h>
#include "stack.h"
#define STACK_DEFAULT_ALLOC_LENGTH 100
/* stack data structure */
struct TStack
{
void* elements; /* data array where each element is of size 'elementSize' */
size_t elementSize; /* size of each element that stack contains */
size_t stackLength; /* length of the stack, items count */
size_t allocatedLength; /* number of allocated elements for stack */
};
/*
Creates stack by given pointer to stack handle.
Allocates memory for structure and default-sized array for elements of given size.
hStackPtr -- pointer to STACK_HANDLE, must not be NULL;
stackElementSize -- size of each element that will be stored in stack in the process,
must be greater than zero;
Return value:
on success returns 'stack_no_error', or:
- 'stack_error_invalid_argument' : if 'hStackPtr' is NULL or 'stackElementSize' is zero
- 'stack_error_mem_alloc' : if function could not allocate memory for anything
*/
int StackCreate(STACK_HANDLE* hStackPtr, const size_t stackElementSize)
{
int errorCode = stack_no_error;
do
{
if ((hStackPtr == NULL) || (stackElementSize == 0))
{
errorCode = stack_error_invalid_argument;
break;
}
(*hStackPtr) = malloc(sizeof(**hStackPtr));
if ((*hStackPtr) == NULL)
{
errorCode = stack_error_mem_alloc;
break;
}
(*hStackPtr)->elements = malloc(STACK_DEFAULT_ALLOC_LENGTH * stackElementSize);
if ((*hStackPtr)->elements == NULL)
{
errorCode = stack_error_mem_alloc;
StackDelete(hStackPtr);
break;
}
else
{
(*hStackPtr)->allocatedLength = STACK_DEFAULT_ALLOC_LENGTH;
(*hStackPtr)->elementSize = stackElementSize;
(*hStackPtr)->stackLength = 0;
}
}
while (0);
return errorCode;
}
/*
Deallocates memory for given 'hStackPtr', destroying stack.
Handle can be reused normally by calling 'StackCreate'.
*/
void StackDelete(STACK_HANDLE* hStackPtr)
{
if ((hStackPtr != NULL) && ((*hStackPtr) != NULL))
{
free((*hStackPtr)->elements);
free(*hStackPtr);
(*hStackPtr) = NULL;
}
}
/*
Checks if given stack by handle is empty (contains zero elements)
*/
int IsStackEmpty(STACK_HANDLE hStack)
{
if (hStack != NULL)
{
return (hStack->stackLength == 0);
}
return 0;
}
/*
Pushes item to stack.
hStack -- stack handle, must be valid
elementAddress -- address of item / element to be pushed to stack, must not be NULL
if actual size of provided element at adress 'elementAddress' does not match
the size of which stack was originally created with, the behaviour is undefined.
Return value:
on success returns 'stack_no_error', or:
- 'stack_error_invalid_handle' : if an invalid handle was supplied
- 'stack_error_invalid_argument' : if 'elementAddress' is NULL
- 'stack_error_mem_alloc' : if function could not allocate memory for anything
*/
int StackPush(STACK_HANDLE hStack, const void* const elementAddress)
{
int errorCode = stack_no_error;
void* tempPtr = NULL;
do
{
if (hStack == NULL)
{
errorCode = stack_error_invalid_handle;
break;
}
if (elementAddress == NULL)
{
errorCode = stack_error_invalid_argument;
break;
}
if (hStack->allocatedLength == hStack->stackLength)
{
/* resizing stack to hold twice as many elements */
hStack->allocatedLength = hStack->allocatedLength * 2;
tempPtr = realloc(hStack->elements, hStack->allocatedLength * hStack->elementSize);
if (tempPtr == NULL)
{
errorCode = stack_error_mem_alloc;
break;
}
hStack->elements = tempPtr;
tempPtr = NULL;
}
/* calculating address to save data */
tempPtr = (char*)hStack->elements + hStack->stackLength * hStack->elementSize;
/* copying data */
memcpy(tempPtr, elementAddress, hStack->elementSize);
hStack->stackLength++;
}
while (0);
return errorCode;
}
/*
Pops item from stack.
hStack -- stack handle, must be valid
elementAddress -- address of item / element to store value popped from stack, must not be NULL
if actual size of provided element at adress 'elementAddress' does not match
the size of which stack was originally created with, the behaviour is undefined.
Return value:
on success returns 'stack_no_error', or:
- 'stack_error_invalid_handle' : if an invalid handle was supplied
- 'stack_error_invalid_argument' : if 'elementAddress' is NULL
- 'stack_error_mem_alloc' : if function could not allocate memory for anything
- 'stack_error_empty_stack' : if stack is empty
*/
int StackPop(STACK_HANDLE hStack, void* const elementAddress)
{
int errorCode = stack_no_error;
const void* sourceAddress = NULL;
if (hStack == NULL)
{
errorCode = stack_error_invalid_handle;
}
else if (elementAddress == NULL)
{
errorCode = stack_error_invalid_argument;
}
else if (IsStackEmpty(hStack))
{
errorCode = stack_error_empty_stack;
}
else
{
hStack->stackLength--;
sourceAddress = (const char*)hStack->elements + hStack->stackLength * hStack->elementSize;
memcpy(elementAddress, sourceAddress, hStack->elementSize);
}
return errorCode;
}
/*
Makes a full copy of the stack.
Both handles must be valid.
Return value:
on success returns 'stack_no_error', or:
- 'stack_error_invalid_handle' : if an invalid handle was supplied
- 'stack_error_invalid_argument' : if 'elementAddress' is NULL
- 'stack_error_mem_alloc' : if function could not allocate memory for anything
- 'stack_error_empty_stack' : if source stack is empty
*/
int StackCopy(STACK_HANDLE hDestinationStack, STACK_HANDLE hSourceStack)
{
int errorCode = stack_no_error;
void* tempPtr = NULL;
do
{
if ((hDestinationStack == NULL) || (hSourceStack == NULL))
{
errorCode = stack_error_invalid_handle;
break;
}
if ((hDestinationStack->elementSize != hSourceStack->elementSize) ||
!IsStackEmpty(hDestinationStack))
{
errorCode = stack_error_invalid_argument;
break;
}
if (IsStackEmpty(hSourceStack))
{
errorCode = stack_error_empty_stack;
break;
}
if (hDestinationStack->allocatedLength != hSourceStack->allocatedLength)
{
/* changing size of dest to match source */
tempPtr = realloc(hDestinationStack->elements, hSourceStack->stackLength * hSourceStack->elementSize);
if (tempPtr == NULL)
{
errorCode = stack_error_mem_alloc;
break;
}
hDestinationStack->elements = tempPtr;
tempPtr = NULL;
}
hDestinationStack->allocatedLength = hSourceStack->allocatedLength;
hDestinationStack->stackLength = hSourceStack->stackLength;
memcpy(hDestinationStack->elements, hSourceStack->elements, hSourceStack->stackLength * hSourceStack->elementSize);
}
while (0);
return errorCode;
} |
|
main.c : 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
| #include <stdio.h>
#include "stack.h"
int main(void)
{
STACK_HANDLE stack1;
STACK_HANDLE stack2;
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int i = 0;
StackCreate(&stack1, sizeof(int));
StackCreate(&stack2, sizeof(int));
for (i = 0; i < sizeof(a) / sizeof(*a); i++) StackPush(stack1, &a[i]);
StackCopy(stack2, stack1);
for (i = 0; i < sizeof(a) / sizeof(*a); i++) StackPop(stack2, &a[i]);
for (i = 0; i < sizeof(a) / sizeof(*a); i++) printf("%d ", a[i]);
StackDelete(&stack1);
StackDelete(&stack2);
return 0;
} |
|
0
|