-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_arrays.c
63 lines (51 loc) · 1.16 KB
/
dynamic_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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "goal_list.h"
#include "dynamic_arrays.h"
goal * new_array (int max_size)
{
goal * array = malloc(max_size * sizeof(goal));
if (array == NULL)
{
printf("Error allocating intial array\n");
return NULL;
}
return array;
}
void double_array_size (goal ** old_array_ptr, int * max_size)
{
goal * old_array = *old_array_ptr;
goal * array = new_array((*max_size) + 20); //make sure this is set up correctly later
if (array == NULL)
{
printf("Error allocating array of double size\n");
return;
}
for (int i = 0; i < *max_size; i++) //copy elements into the new array
{
array[i] = old_array[i];
}
*max_size = (*max_size) * 2;
free(old_array);
*old_array_ptr = array;
}
void add_element(goal ** array_ptr, int * max_size, int * size, goal element)
{
if (*size >= *max_size)
{
double_array_size(array_ptr, max_size);
}
goal * array = *array_ptr;
array[*size] = element;
*size = *size + 1;
}
void remove_element(goal array[], int * size, int index)
{
*size = *size - 1;
while (index < *size)
{
array[index] = array[index + 1];
index++;
}
}