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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Person
{
char name[64];
int age;
};
//动态数组结构体
struct dynamiArray
{
//真实在堆区开辟数组的指针
void** p1;
//数组容量
int m_Capacity;
//数组长度
int m_Size;
};
//初始化数组
struct dynamiArray * init_dynamicArray(int capacity)
{
if (capacity <= 0) {
return NULL;
}
//给数组分配内存
struct dynamiArray* arr = malloc(sizeof(struct dynamiArray));
if (arr == NULL) {
return NULL;
}
//给数组初始化
arr->p1 = malloc(sizeof(void*) * capacity);
arr->m_Capacity = capacity;
arr->m_Size = 0;
return arr;
};
//插入数据
void insert_dynamiArray(struct dynamiArray* arr, void* data, int pos)
{
if (arr == NULL)
{
return;
}
if (data == NULL)
{
return;
}
if (pos<0 || pos>arr->m_Size)
{
//尾插
pos = arr->m_Size;
}
//判断数组是否满了
if (arr->m_Size == arr->m_Capacity)
{
//计算新内存空间大小
int newCapacity = arr->m_Capacity * 2;
//开辟新的空间
void** newsPace = malloc(sizeof(void*) * newCapacity);
//将原空间的数据拷贝到新空间下
memcpy(newsPace, arr->p1, sizeof(void*)* arr->m_Capacity);
//释放原空间
free(arr->p1);
//更改指针指向
arr->p1 = newsPace;
//更新新的容量
arr->m_Capacity = newCapacity;
}
for (int i = arr->m_Size; i >pos; i--)
{
arr->p1[i] = arr->p1[i-1];
}
arr->p1[pos] = data;
arr->m_Size++;
}
//按照位置删除数据
void removeByPos_dynamiArray(struct dynamiArray* arr, int pos)
{
if(arr==NULL)
{
return;
}
//无效的删除位置
if(pos<0||pos>arr->m_Size-1)
{
return;
}
for (int i = pos; i < arr->m_Size; i++)
{
arr->p1[i] = arr->p1[i + 1];
}
arr->m_Size--;
}
int comparePerson(void *data1,void*data2)
{
struct Person* p1 = data1;
struct Person* p2 = data2;
return strcmp(p1->name, p2->name) == 0 && p1->age == p2->age;
}
//按照值删除
void removeByValue_dynamiArray(struct dynamiArray* arr, void* data, int(*myCompare)(void*, void*))
{
if(arr==NULL)
{
return;
}
if(data==NULL)
{
return;
}
for (int i = 0; i < arr->m_Size; i++)
{
if (myCompare(arr->p1[i], data))
{
removeByPos_dynamiArray(arr, i);
break;
}
}
}
//遍历
void foreach_dynamiArray(struct dynamiArray* arr,void(*myPrint)(void*))
{
if(arr==NULL)
{
return;
}
for (int i = 0; i < arr->m_Size; i++)
{
myPrint(arr->p1[i]);
}
}
void printPerson(void* data)
{
struct Person* person = data;
printf("姓名: %s 年龄: %d \n", person->name, person->age);
}
void destory_dynamiArray(struct dynamiArray* arr)
{
if (arr == NULL)
{
return;
}
if (arr->p1 != NULL)
{
free(arr->p1);
arr->p1 = NULL;
}
free(arr);
arr = NULL;
return;
}
void test01()
{
struct dynamiArray* arr = init_dynamicArray(3);
struct Person p1 = { "赵云",18 };
struct Person p2 = { "关羽",19 };
struct Person p3 = { "张飞",20 };
struct Person p4 = { "刘备",21 };
printf("插入数据前,数组的容量为 %d\n", arr->m_Capacity);
printf("插入数据前,数组的大小为 %d\n", arr->m_Size);
insert_dynamiArray(arr, &p1, 0);
insert_dynamiArray(arr, &p2, -1);
insert_dynamiArray(arr, &p3, 0);
insert_dynamiArray(arr, &p4, -1);
foreach_dynamiArray(arr, printPerson);
/* 姓名: 张飞 年龄 : 20
姓名 : 赵云 年龄 : 18
姓名 : 关羽 年龄 : 19
姓名 : 刘备 年龄 : 2*/
printf("插入数据后,数组的容量为 %d\n", arr->m_Capacity);
printf("插入数据后,数组的大小为 %d\n", arr->m_Size);
printf("删除关羽\n");
removeByPos_dynamiArray(arr, 2);
foreach_dynamiArray(arr, printPerson);
printf("删除刘备\n");
struct Person p = { "刘备",21 };
removeByValue_dynamiArray(arr, &p, comparePerson);
foreach_dynamiArray(arr, printPerson);
}
int main()
{
test01();
system("pause");
return 0;
}
|