-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThe dynamic array.cpp
88 lines (88 loc) · 1.77 KB
/
The dynamic array.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# person-chengyu
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stddef.h>
struct Vector{
int* m_base;
int m_len;
int m_capacity;
};
typedef struct Vector vector;
typedef int Type;
void InitVector(vector* v){
v->m_base=NULL;
v->m_len=0;
v->m_capacity=0;
}//初始化
void destroyVector(vector * v){
if(v->m_base!=NULL){
free(v->m_base);
}
}//销毁
void pushBackVector(vector* v,Type value){
if(v->m_len<v->m_capacity){
v->m_base[v->m_len]=value;
v->m_len++;
}else {
if(v->m_base==NULL){
v->m_base=(Type*)malloc(sizeof(Type));
if(v->m_base=NULL){
printf("Error\n");
return ;
}
v->m_capacity=1;
}
else{
Type* new_base=(Type*)malloc(sizeof(Type)*v->m_capacity*2);
if(new_base=NULL){
printf("Error\n");
return ;
}
memcpy(new_base,v->m_base,sizeof(Type)*(v->m_capacity));
free(v->m_base);
v->m_base=new_base;
v->m_capacity*=2;
}
v->m_base[v->m_len]=value;
v->m_len++;
}
}//从数组后插入元素
void popBackVector(vector* v){
if(v->m_base=NULL){
printf("Error\n");
return ;
}
if(v->m_len==1){
v->m_base=NULL;
v->m_len=0;
v->m_capacity=0;
}else if(v->m_len-1==v->m_capacity/2){
Type* new_base=(Type*)malloc(sizeof(Type)*v->m_len);
if(new_base=NULL){
printf("Error\n");
return ;
}
memcpy(new_base,v->m_base,v->m_len);
free(v->m_base);
v->m_base=new_base;
v->m_len--;
v->m_capacity=v->m_len;
}
else{
v->m_len--;
}
}//从数组后拿出元素
Type * getPos(vector * v , Type pos){
if(pos >= v -> m_len){
return NULL;
}
return v -> m_base + pos;
}//数组位置的值
Type sizeVector(vector * v){
return v -> m_len;
}
Type capacityVector(vector * v){
return v -> m_capacity;
}
/******************************************************/