forked from Minerkow/Semestr-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
126 lines (108 loc) · 2.39 KB
/
Stack.cpp
File metadata and controls
126 lines (108 loc) · 2.39 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
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <assert.h>
struct Stack_t
{
char* data;
size_t size;
int error;
};
bool StackPush(Stack_t *stack, char new_elem);
char StackPop(Stack_t *stack);
void AddMem(Stack_t *stack);
void DellMem (Stack_t *stack);
bool StackOK (Stack_t *stack);
void Dump (Stack_t *stack);
const int MAXSIZE = 10000;
const int MINSIZE = 1;
const int NULLPTR = 228;
const int BOTTOM_EXIT = 69;
const int TOP_EXIT = 1337;
const int FAIL = 1;
const int SUCCESS = 0;
const int SLOT = 1;
int main()
{
Stack_t stack = {NULL, 0, 0};
stack.data = (char*)calloc(MINSIZE, sizeof(char));//Тут я создаю calloc нулевого размера , может быть лажа
StackPush (&stack, 28);
StackPush (&stack, 123);
StackPush (&stack, 13);
int b = StackPop(&stack);
StackPush (&stack, 21);
printf("TOP - %d\n", b);
for (int i = 0; i < stack.size; i++)
printf ("Elem[%d] = %d\n", i, stack.data[i]);
return 0;
}
bool StackPush(Stack_t *stack, char new_elem)
{
if (StackOK)
Dump(stack);
AddMem(stack);
stack->data[stack->size++] = new_elem;
if(StackOK)
Dump(stack);
return SUCCESS;
}
char StackPop(Stack_t *stack)
{
if (StackOK(stack))
Dump(stack);
char top = stack->data[stack->size - 1];
// printf ("%d", top);
DellMem(stack);
stack->size--;
return top;
}
void AddMem(Stack_t *stack)
{
stack->data = (char*)realloc(stack->data, stack->size + SLOT);
}
void DellMem (Stack_t *stack)
{
stack->data = (char*)realloc(stack->data, stack->size - SLOT);
}
bool StackOK (Stack_t *stack)
{
assert(stack);
if (stack->data == NULL)
{
stack->error = NULLPTR;
return FAIL;
}
if (stack->size < MINSIZE)
{
stack->error = BOTTOM_EXIT;
return FAIL;
}
if (stack->size > MAXSIZE )
{
stack->error = TOP_EXIT;
return FAIL;
}
return SUCCESS;
}
void Dump(Stack_t *stack)
{
//printf("Size: %d", stack->size);
switch (stack->error)
{
case 69:
{
printf("Going beyond the bottom of the array");
break;
}
case 1337:
{
printf("Exceeding the upper bound of the array");
break;
}
case 228:
{
printf("Array error");
break;
}
}
}