-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgrowing_arrays.c
201 lines (172 loc) · 4.38 KB
/
growing_arrays.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Nameval
{
char *name;
int value;
} Nameval;
struct NVtab
{
int nval; /* current number of values */
int max; /* allocated number of values*/
Nameval *nameval; /* array of name-value pairs*/
} nvtab;
enum
{
NVINIT = 1,
NVGROW = 2
};
int delname(char *name);
int delname_no_memmove(char *name);
int delname_marked(char *name);
int addname(Nameval newname);
int addname_marked(Nameval newname);
void print_nvtab();
int main()
{
Nameval *n = (Nameval *)malloc(sizeof(Nameval));
(*n).name = "Ivan";
(*n).value = 1;
addname(*n);
(*n).name = "Georgi";
(*n).value = 2;
addname(*n);
(*n).name = "Anton";
(*n).value = 4;
addname(*n);
(*n).name = "Pesho";
(*n).value = 4;
addname(*n);
(*n).name = "Dragan";
(*n).value = 5;
addname(*n);
(*n).name = "Petkan";
(*n).value = 6;
addname(*n);
print_nvtab();
delname("Dragan");
printf("after deleting Dragan\n");
print_nvtab();
delname("Dragan");
delname_no_memmove("Petkan");
printf("after deleting Petkan\n");
print_nvtab();
delname_marked("Georgi");
(*n).name = "Gosho";
(*n).value = 5;
addname_marked(*n);
printf("Gosho should be in second place, instead of Georgi\n");
print_nvtab();
return 0;
}
void print_nvtab()
{
for (int i = 0; i < nvtab.nval; i++)
{
printf("%s-%d, ", nvtab.nameval[i].name, nvtab.nameval[i].value);
}
printf("\n");
}
/* addname: add new name and value to nvtab */
int addname(Nameval newname)
{
Nameval *nvp;
if (nvtab.nameval == NULL) /* first time */
{
printf("initializing nvtab.nameval\n");
nvtab.nameval = (Nameval *)malloc(NVINIT * sizeof(Nameval));
if (nvtab.nameval == NULL)
return -1; /* not enough memory to allocate */
nvtab.max = NVINIT;
nvtab.nval = 0;
}
else if (nvtab.nval == nvtab.max) /* need to grow the array */
{
printf("growing nvtab.nameval - new size: %d\n", NVGROW * nvtab.max);
nvp = (Nameval *)realloc(nvtab.nameval, (NVGROW * nvtab.max) * sizeof(Nameval));
if (nvp == NULL)
return -1; /* not enough memory to reallocate */
nvtab.max *= NVGROW;
nvtab.nameval = nvp;
}
nvtab.nameval[nvtab.nval] = newname;
return nvtab.nval++;
}
int delname(char *name)
{
for (int i = 0; i < nvtab.nval; i++)
{
if (strcmp(nvtab.nameval[i].name, name) == 0)
{
memmove(nvtab.nameval + i, nvtab.nameval + i + 1, (nvtab.nval - (i + 1)) * sizeof(Nameval));
nvtab.nval--;
return 1;
}
}
return 0;
}
int delname_no_memmove(char *name)
{
for (int i = 0; i < nvtab.nval; i++)
{
if (strcmp(nvtab.nameval[i].name, name) == 0)
{
for (int j = i; j < nvtab.nval; j++)
{
nvtab.nameval[j] = nvtab.nameval[j + 1];
}
nvtab.nval--;
return 1;
}
}
return 0;
}
int addname_marked(Nameval newname)
{
Nameval *nvp;
if (nvtab.nameval == NULL) /* first time */
{
printf("initializing nvtab.nameval\n");
nvtab.nameval = (Nameval *)malloc(NVINIT * sizeof(Nameval));
if (nvtab.nameval == NULL)
return -1; /* not enough memory to allocate */
nvtab.max = NVINIT;
nvtab.nval = 0;
}
else
{
for (int i = 0; i < nvtab.nval; i++)
{
if (nvtab.nameval[i].name == NULL)
{
nvtab.nameval[i] = newname;
return nvtab.nval++;
}
}
}
if (nvtab.nval == nvtab.max) /* need to grow the array */
{
printf("growing nvtab.nameval - new size: %d\n", NVGROW * nvtab.max);
nvp = (Nameval *)realloc(nvtab.nameval, (NVGROW * nvtab.max) * sizeof(Nameval));
if (nvp == NULL)
return -1; /* not enough memory to reallocate */
nvtab.max *= NVGROW;
nvtab.nameval = nvp;
}
nvtab.nameval[nvtab.nval] = newname;
return nvtab.nval++;
}
int delname_marked(char *name)
{
for (int i = 0; i < nvtab.nval; i++)
{
if (strcmp(nvtab.nameval[i].name, name) == 0)
{
nvtab.nameval[i].name = NULL;
nvtab.nval--;
return 1;
}
}
return 0;
}