-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyVec.cpp
65 lines (50 loc) · 1.38 KB
/
myVec.cpp
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct vec_int
{
int* data;
int size;
};
struct vec_int * create_vec_int(void);
void push_vec_int(struct vec_int*, int);
void show_vec_int(struct vec_int*);
void destroy_vec_int(struct vec_int*);
int main(void)
{
struct vec_int * vec_int_ver = NULL;
vec_int_ver = create_vec_int();
for(int i = 0; i < 10; i++)
{
push_vec_int(vec_int_ver,(i+1)*10);
}
show_vec_int(vec_int_ver);
destroy_vec_int(vec_int_ver);
return(0);
}
struct vec_int * create_vec_int(void)
{
struct vec_int * temp_struct_vec_int = NULL;
temp_struct_vec_int = (struct vec_int*)malloc(sizeof(struct vec_int));
memset(temp_struct_vec_int,0,sizeof(struct vec_int));
return(temp_struct_vec_int);
}
void push_vec_int(struct vec_int* temp_struct_vec_int, int new_data)
{
temp_struct_vec_int->data = (int*)realloc(temp_struct_vec_int->data, ((temp_struct_vec_int->size) + 1)* sizeof(int));
temp_struct_vec_int->size = temp_struct_vec_int->size + 1;
temp_struct_vec_int->data[(temp_struct_vec_int->size) - 1] = new_data;
}
void show_vec_int(struct vec_int* temp_struct_vec_int)
{
for(int i = 0; i < temp_struct_vec_int->size; i++)
{
printf("vec_int_ver[%d] : %d \n", i, temp_struct_vec_int->data[i]);
}
}
void destroy_vec_int(struct vec_int* temp_struct_vec_int)
{
free(temp_struct_vec_int->data);
free(temp_struct_vec_int);
temp_struct_vec_int = NULL;
}