Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f17570f

Browse files
committed
fnplus#565 adding Dynamic and generic Array in C
1 parent f2b5302 commit f17570f

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#define INITIAL_CAPACITY 10
5+
#define customFor(i,n) for(i = 0 ; i< n ; i++)
6+
typedef struct Element{
7+
void* value;
8+
} Element;
9+
typedef struct Array{
10+
Element* array;
11+
int size;
12+
int capacity;
13+
} Array;
14+
struct Array createArray(){
15+
Array array;
16+
array.capacity = INITIAL_CAPACITY;
17+
array.size = 0;
18+
array.array = calloc(array.capacity, sizeof(Element));
19+
return array;
20+
}
21+
void add(Array* array, void* value){
22+
if((array->size+1) > array->capacity){
23+
array->capacity *= 2;
24+
array->array = realloc(array->array,array->capacity* sizeof(Element));
25+
}
26+
Element *element = malloc(sizeof(Element));
27+
element->value = value;
28+
array->array[array->size] = *element;
29+
array->size ++;
30+
}
31+
void removeElement(Array *array,int i){
32+
memmove(&array->array[i],&array->array[i+1],(array->size-i)* sizeof(Element));
33+
array->size--;
34+
}
35+
void* get(Array *array,int i){
36+
return array->array[i].value;
37+
}
38+
void forEach(Array *array, void (*action)(void *)) {
39+
for (int i = 0; i < array->size; ++i) {
40+
action(array->array[i].value);
41+
}
42+
}
43+
void testIntegerArray();
44+
void testCharArray();
45+
void printIntPtrValue(int* value){
46+
printf("%d ",*value);
47+
}
48+
void printCharPtrValue(char* value){
49+
printf("%s ",value);
50+
}
51+
int main() {
52+
testIntegerArray();
53+
testCharArray();
54+
return 0;
55+
}
56+
void testIntegerArray(){
57+
Array integerArray = createArray();
58+
printf("Testing Dynamic Array with int elements\n");
59+
printf("Initial capacity= %d\n", integerArray.capacity);
60+
printf("Size= %d\n", integerArray.size);
61+
printf("Adding twenty integers\n");
62+
for (int i=0;i<20;i++){
63+
int *x = malloc(sizeof(int));
64+
*x=i;
65+
add(&integerArray, x);
66+
}
67+
printf("Size= %d\n", integerArray.size);
68+
printf("Printing Dynamic Array with forEach : \n");
69+
forEach(&integerArray, printIntPtrValue);
70+
printf("\nDeleting element at index 7 and 19...\n");
71+
removeElement(&integerArray,7);
72+
removeElement(&integerArray,19);
73+
printf("Size= %d\n", integerArray.size);
74+
printf("Printing Dynamic Array with for loop: \n");
75+
int i =0;
76+
customFor(i,integerArray.size){
77+
printIntPtrValue(get(&integerArray,i));
78+
}
79+
}
80+
void testCharArray() {
81+
printf("\n");
82+
printf("-------------------------------------------------------\n");
83+
Array charArray = createArray();
84+
printf("Testing Dynamic Array with chars elements\n");
85+
printf("Initial capacity= %d\n", charArray.capacity);
86+
printf("Size= %d\n", charArray.size);
87+
printf("Adding some char\n");
88+
add(&charArray,"C");
89+
add(&charArray,"C++");
90+
add(&charArray,"PHP");
91+
add(&charArray,"Java");
92+
add(&charArray,"Javascript");
93+
add(&charArray,"C#");
94+
printf("Size= %d\n", charArray.size);
95+
printf("Printing Dynamic Array with forEach : \n");
96+
forEach(&charArray, printCharPtrValue);
97+
printf("\nDeleting element at index 2 and 5...\n");
98+
removeElement(&charArray,2);
99+
removeElement(&charArray,5);
100+
printf("Size= %d\n", charArray.size);
101+
printf("Printing Dynamic Array with for loop: \n");
102+
int i =0;
103+
customFor(i,charArray.size){
104+
printCharPtrValue(get(&charArray,i));
105+
}
106+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /