Repository navigation
Expand file tree
/
Copy patharray.h
More file actions
90 lines (79 loc) · 2.4 KB
/
Copy patharray.h
File metadata and controls
90 lines (79 loc) · 2.4 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
#ifndef ARRAY_JL
#define ARRAY_JL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define ARRAY_MAX_SIZE 100 // The maximum number of elements that we can store in the array.
// Define how our (linear and static) array is structured.
typedef struct{
int data[ARRAY_MAX_SIZE]; // The internal array to store our elements.
int size; // The current number of elements.
} Array;
/********************/
/* FUNCTIONS */
/********************/
// Set Up the Array. We start with size 0 to show it's empty.
void initArray(Array *array){
array->size = 0;
}
// Add an Element to the End of the Array. Returns 1 if successful or 0 if there's no space left.
int insertArray(Array *array, int value){
if(array->size >= ARRAY_MAX_SIZE || array->size < 0){
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 is full.
int insertAt(Array *array, int index, int value){
if(array->size >= ARRAY_MAX_SIZE || index < 0 || index > array->size){
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 index is not valid.
int deleteAt(Array *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 index is invalid.
int getElement(Array *array, int index, int *value){
if(index < 0 || index >= array->size){
return 0;
}
else{
*value = array->data[index];
return 1;
}
}
// Print all Elements of the Array. Displays elements in the following format: [1, 2, 3, 4, 5]
void printArray(Array *array){
printf("[");
for(int i = 0; i < array->size; i++){
printf("%d", array->data[i]);
if(i < array->size - 1){
printf(", ");
}
}
printf("]\n");
}
#endif //ARRAY_JL