-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
127 lines (103 loc) · 2.93 KB
/
test.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
#include <stdio.h>
#include <stdlib.h>
typedef struct arrayList
{
int size;
int index;
int elements;
int avaliableMemory;
void *array;
} arrayList;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Intialisation of arrayList
void intialise_arrayList(struct arrayList *a1, int arraySize)
{
a1->array = calloc(arraySize, sizeof(double)); // 8 bytes
if (a1->array == NULL)
{
exit(1);
}
a1->size = arraySize; //How many elements can be stored in the array
a1->index = 0; // The current index
a1->avaliableMemory = arraySize * sizeof(double); // The amount of memory the array has
a1->elements = 0;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Add elemet to arrayList
void add_element(struct arrayList *a1, int element)
{
void *status = NULL;
// No longer any space in the arrayList
if (a1->index == a1->size)
{
// Reallocates memory (no cast as realloc returns void pointer by default)
status = realloc(a1->array, a1->size * sizeof(double));
//Checks for failure in memory allocation
if (status == NULL)
{
exit(1);
}
// Inserts specified element
((double *)(a1-> array))[a1->index++] = (double) element;
a1->size = a1->size * 2;
a1->avaliableMemory = a1->avaliableMemory * 2;
}
else
{
((double *)(a1-> array))[a1->index++] = (double) element;
}
a1->elements++;
}
double cast_helper(int element)
{
return (double) element;
}
void arraylist_insert(struct arrayList *a1, int element, int index)
{
void *pointer = NULL;
pointer = malloc(a1->size * sizeof(double));
if (pointer == NULL)
{
exit(1);
}
for (int i = 0; i < index; i++)
{
((double *) pointer)[i] = ((double *)(a1->array))[i];
}
a1->elements++;
// Insert the double element
((double *)pointer)[index] = (double)element;
for (int i = index + 1, x = index; i < a1->elements + 1; i++, x++)
{
((double *)pointer)[i] = ((double *)(a1->array))[x];
}
// Releases the memory of the previous arrayList
free(a1->array);
// Assigns the new pointer
a1->array = pointer;
}
void displayElements(struct arrayList *a1)
{
for (int i = 0; i < a1->elements; i++)
{
// if (a1->array[i] != 0)
// {
// printf("%d ", a1->array[i]);
// }
printf("%f ", ((double *)a1->array)[i]);
}
}
int main(int argc, char const *argv[])
{
arrayList array;
int element = 20;
intialise_arrayList(&array, 3);
add_element(&array, 10);
add_element(&array, 10);
add_element(&array, 10);
add_element(&array, 10);
arraylist_insert(&array, 20, 2);
displayElements(&array);
getchar();
return 0;
}