-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynarray.c
More file actions
274 lines (240 loc) · 6.69 KB
/
dynarray.c
File metadata and controls
274 lines (240 loc) · 6.69 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
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
266
267
268
269
270
271
272
273
274
/*
* This file contains a simple implementation of a dynamic array. See the
* documentation below for more information on the individual functions in
* this implementation.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "dynarray.h"
/*
* This structure is used to represent a single dynamic array.
*/
struct dynarray {
void** data;
int size;
int capacity;
int front;
};
#define DYNARRAY_INIT_CAPACITY 4
/*
* This function allocates and initializes a new, empty dynamic array and
* returns a pointer to it.
*/
struct dynarray* dynarray_create() {
struct dynarray* da = malloc(sizeof(struct dynarray));
assert(da);
da->data = malloc(DYNARRAY_INIT_CAPACITY * sizeof(void*));
assert(da->data);
da->size = 0;
da->front = 0;
da->capacity = DYNARRAY_INIT_CAPACITY;
return da;
}
/*
* This function frees the memory associated with a dynamic array. Freeing
* any memory associated with values stored in the array is the responsibility
* of the caller.
*
* Params:
* da - the dynamic array to be destroyed. May not be NULL.
*/
void dynarray_free(struct dynarray* da) {
assert(da);
free(da->data);
free(da);
}
/*
* This function returns the size of a given dynamic array (i.e. the number of
* elements stored in it, not the capacity).
*/
int dynarray_size(struct dynarray* da) {
assert(da);
return da->size;
}
/*
* Auxilliary function to perform a resize on a dynamic array's underlying
* storage array.
*/
void _dynarray_resize(struct dynarray* da, int new_capacity) {
assert(new_capacity > da->size);
/*
* Allocate space for the new array.
*/
void** new_data = malloc(new_capacity * sizeof(void*));
assert(new_data);
/*
* Initialize every element to NULL
*/
for (int i = 0; i < new_capacity; i++) {
new_data[i] = NULL;
}
/*
* Copy data from the old array to the new one.
*/
for (int i = 0; i < da->size; i++) {
new_data[i] = da->data[(i + da->front) % da->capacity];
}
/*
* Put the new array into the dynarray struct.
*/
free(da->data);
da->data = new_data;
da->capacity = new_capacity;
}
/*
* This function inserts a new value to a given dynamic array. The new element
* is always inserted at the *end* of the array.
*
* Params:
* da - the dynamic array into which to insert an element. May not be NULL.
* val - the value to be inserted. Note that this parameter has type void*,
* which means that a pointer of any type can be passed.
*/
void dynarray_insert(struct dynarray* da, void* val) {
assert(da);
/*
* Make sure we have enough space for the new element. Resize if needed.
*/
if (da->size == da->capacity) {
_dynarray_resize(da, 2 * da->capacity);
}
/*
* Put the new element at the end of the array.
*/
da->data[da->size] = val;
da->size++;
}
/*
* This function removes an element at a specified index from a dynamic array.
* All existing elements following the specified index are moved forward to
* fill in the gap left by the removed element.
*
* Params:
* da - the dynamic array from which to remove an element. May not be NULL.
* idx - the index of the element to be removed. The value of `idx` must be
* between 0 (inclusive) and n (exclusive), where n is the number of
* elements stored in the array.
*/
void dynarray_remove(struct dynarray* da, int idx) {
assert(da);
assert(idx < da->size && idx >= 0);
/*
* Move all elements behind the one being removed forward one index,
* overwriting the element to be removed in the process.
*/
for (int i = idx; i < da->size - 1; i++) {
da->data[i] = da->data[i+1];
}
da->size--;
}
/*
* This function returns the value of an existing element in a dynamic array.
*
* Params:
* da - the dynamic array from which to get a value. May not be NULL.
* idx - the index of the element whose value should be returned. The value
* of `idx` must be between 0 (inclusive) and n (exclusive), where n is the
* number of elements stored in the array.
*/
void* dynarray_get(struct dynarray* da, int idx) {
assert(da);
assert(idx < da->size && idx >= 0);
return da->data[idx];
}
/*
* This function updates (i.e. overwrites) the value of an existing element in
* a dynamic array.
*
* Params:
* da - the dynamic array in which to set a value. May not be NULL.
* idx - the index of the element whose value should be updated. The value
* of `idx` must be between 0 (inclusive) and n (exclusive), where n is the
* number of elements stored in the array.
* val - the new value to be set. Note that this parameter has type void*,
* which means that a pointer of any type can be passed.
*/
void dynarray_set(struct dynarray* da, int idx, void* val) {
assert(da);
assert(idx < da->size && idx >= 0);
da->data[idx] = val;
}
/*
* helper function for testing, do not modify
*/
/*
* This function prints out all spots (from 0 to capacity) in a dynamic array.
* If the spot does not have an element, print NULL.
*
* Params:
* da - the dynamic array from which to print. May not be NULL.
* void (*p) (void* a) - a function pointer to print an element within the queue
*
* Return:
* none.
*/
void dynarray_print(struct dynarray* da, void (*p) (void* a)){
assert(da);
assert(da->data);
for (int i = 0; i < da->capacity; ++i)
{
printf("%d: ", i);
if (da->data[i] == NULL){
printf("NULL\n");
continue;
}
p(da->data[i]);
}
return;
}
/*
* This funtion should simulate the enqueue function which enters a data type into the queue at the back.
*
* Params:
* da - dynarray struct
* val - void* value to enter queue
*
* Return:
* none.
*/
void dynarray_enqueue(struct dynarray* da, void* val){
if(da->size >= da->capacity){
int new_capacity = 2* da->capacity;
_dynarray_resize(da, new_capacity);
da->data[da->size] = val; // add value to end of array/back of queue
da->front = 0; // set front back to beginning of array
}
else{
da->data[(da->front + da->size) % da->capacity]= val;
}
da->size++;
return;
}
/*
* This function should return the item/struct at the front of the queue.
*
* Params:
* da - dynarray struct
*
* Return:
* This function will return the value at the front of the queue
*/
void* dynarray_get_front(struct dynarray* da){
return da->data[da->front];
}
/*
* This funtion should remove the value/struct at end of the queue with O(1) and set it to NULL.
*
* Params:
* da - the dynamic array from which to print. May not be NULL.
*
* Return:
* value removed
*/
void* dynarray_dequeue(struct dynarray* da){
da->size--;
void* val = da->data[da->front];
da->data[da->front] = NULL;
da->front = (da->front + 1) % da->capacity;
return val;
}