-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_m_A.c
More file actions
145 lines (121 loc) · 2.9 KB
/
stack_m_A.c
File metadata and controls
145 lines (121 loc) · 2.9 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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#define ALLOC_ARR(arr, count) ((arr) = malloc((count) * sizeof ((arr)[0])))
#define ALLOC_PTR(ptr) ALLOC_ARR(ptr, 1)
struct stack {
size_t max; // maximum elements in this stack
size_t count; // number of free elements in stack
int *stack_store; // elements
};
int stack_init(struct stack *s, int count);
void stack_fini(struct stack *s);
int stack_pop(struct stack *s);
void stack_push(struct stack *s, int elem);
int stack_init(struct stack *s, int count)
{
// s->count = count-1;//count - 1 because malloc(100) is from 0 to 99
s->count = count;
s->max = count; //if max used only with <
ALLOC_ARR(s->stack_store, s->count);
// s->stack_store = (int*)malloc(s->count * (sizeof(int)));
return s == NULL ? -ENOMEM : 0;
}
void stack_fini(struct stack *s)
{
free(s->stack_store);
s->stack_store = NULL;
}
void stack_push(struct stack *s, int elem)
{
assert(s->count > 0);
/*
if(s->count == 0)
// if(s->count < 0) // size_t
return;
*/
// printf("%i ", s->stack_store[s->count]);
s->stack_store[--s->count] = elem; //here error --s->count
//printf("s->count value: %zu ", s->count);
}
int stack_pop(struct stack *s)
{
assert(s->count < s->max);
/*
if (s->count < s->max)
return; // problem with idintification of error
*/
// printf("%i ", s->stack_store[s->count]);
//______________________del_test
int temp = s->stack_store[s->count];
s->stack_store[s->count++] = -1;
return temp;
//______________________del_test
// return s->stack_store[s->count++];
}
void stack_print(struct stack *s)
{
printf("max: %zu\n", s->max);
printf("count: %zu\n", s->count);
for(size_t i = 0; i < s->max; ++i) {
printf("%i ", s->stack_store[i]);
}
}
int main(int argc, char** argv)
{
int rc;
struct stack *s;
// s = (struct stack *)malloc(sizeof(struct stack));
ALLOC_PTR(s);
assert(s != NULL);
/*
if (s == 0) {
perror("error malloc");
return -ENOMEM;
}
*/
rc = stack_init(s, 100);
assert(rc == 0);
/*
if (rc != 0) {
perror("error init");
return rc; //not (-rc) because init return value with -
}
*/
// for (size_t i = 0; i < 100; ++i)
for (size_t i = 0; i < s->max; ++i)
stack_push(s, i); // push 2 times 0 in start
stack_print(s);
printf("\n");
// for (size_t i = 99; i > 10; --i) {
for (size_t i = s->max-1; i > 10; --i) { //maybe (s->max-1)
rc = stack_pop(s);
assert(rc == i); // wrong in some cases
/*
if (rc != i) {
//hz, problem with return
printf("%i\n", rc);
fprintf(stderr ,"problem with return value\n");
return -1;
}
*/
}
printf("after pop\n");
stack_print(s);
printf("\n");
size_t temp = s->count;
for (size_t i = 0; i < temp; ++i)
stack_push(s, i);
/*
for (size_t i = s->count; i > 0; --i)
stack_push(s, i);
*/
printf("count after second push: %zu \n", s->count);
stack_print(s);
printf("\n");
stack_fini(s);
free(s);
s = 0;
return 0;
}