Repository navigation
Expand file tree
/
Copy pathdynamicarray.h
More file actions
139 lines (125 loc) · 4.03 KB
/
Copy pathdynamicarray.h
File metadata and controls
139 lines (125 loc) · 4.03 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
#ifndef DYNAMICARRAY_JL
#define DYNAMICARRAY_JL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define DYNAMIC_ARRAY_INITIAL_CAPACITY 4 // The number of elements that we reserve when the array is created.
// Define how our (linear and dynamic) array is structured. The memory grows with realloc when it's full.
typedef struct{
int* data; // The pointer to the memory where we store our elements.
int size; // The current number of elements.
int capacity; // The number of elements that fit in the reserved memory.
} DynamicArray;
/********************/
/* FUNCTIONS */
/********************/
// Set Up the Dynamic Array. Reserves memory for the initial capacity. Returns 1 if successful or 0 if there's no memory.
int initDynamicArray(DynamicArray *array){
array->data = (int*)malloc(DYNAMIC_ARRAY_INITIAL_CAPACITY * sizeof(int));
array->size = 0;
if(!array->data){
array->capacity = 0;
return 0;
}
else{
array->capacity = DYNAMIC_ARRAY_INITIAL_CAPACITY;
return 1;
}
}
// Double the Capacity of the Array. Returns 1 if successful or 0 if there's no memory (the old data is kept).
int growDynamicArray(DynamicArray *array){
int newCapacity = array->capacity * 2;
if(newCapacity == 0){
newCapacity = DYNAMIC_ARRAY_INITIAL_CAPACITY;
}
int* newData = (int*)realloc(array->data, newCapacity * sizeof(int));
if(!newData){
return 0;
}
else{
array->data = newData;
array->capacity = newCapacity;
return 1;
}
}
// Add an Element to the End of the Array. Returns 1 if successful or 0 if the array can't grow.
int pushBackDynamicArray(DynamicArray *array, int value){
if(array->size == array->capacity && !growDynamicArray(array)){
return 0;
}
else{
array->data[array->size] = value;
array->size++;
return 1;
}
}
// Add an Element at a Specific Position of the Array. Returns 1 if successful or 0 if the index is invalid or the array can't grow.
int insertAtDynamicArray(DynamicArray *array, int index, int value){
if(index < 0 || index > array->size){
return 0;
}
if(array->size == array->capacity && !growDynamicArray(array)){
return 0;
}
else{
for(int i = array->size; i > index; i--){
array->data[i] = array->data[i-1];
}
array->data[index] = value;
array->size++;
return 1;
}
}
// Delete an Element at a Specific Position of the Array. Returns 1 if successful or 0 if the index is invalid.
int deleteAtDynamicArray(DynamicArray *array, int index){
if(index < 0 || index >= array->size){
return 0;
}
else{
for(int i = index; i < array->size-1; i++){
array->data[i] = array->data[i+1];
}
array->size--;
return 1;
}
}
// Get an Element from a Specific Position of the Array. Returns 1 if successful or 0 if the index is invalid.
int getDynamicArray(DynamicArray *array, int index, int *value){
if(index < 0 || index >= array->size){
return 0;
}
else{
*value = array->data[index];
return 1;
}
}
// Change the Element at a Specific Position of the Array. Returns 1 if successful or 0 if the index is invalid.
int setDynamicArray(DynamicArray *array, int index, int value){
if(index < 0 || index >= array->size){
return 0;
}
else{
array->data[index] = value;
return 1;
}
}
// Print all Elements of the Array. Displays elements in the following format: [1, 2, 3, 4, 5]
void printDynamicArray(DynamicArray *array){
printf("[");
for(int i = 0; i < array->size; i++){
printf("%d", array->data[i]);
if(i < array->size-1){
printf(", ");
}
}
printf("]\n");
}
// Free the Memory of the Array and Reset It.
void freeDynamicArray(DynamicArray *array){
free(array->data);
array->data = NULL;
array->size = 0;
array->capacity = 0;
}
#endif //DYNAMICARRAY_JL