-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.c
107 lines (96 loc) · 1.84 KB
/
vector.c
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
#include<stdio.h>
#include<stdlib.h>
#include<stddef.h>
#include<malloc.h>
#include<string.h>
struct Vector{
int *array;
int len;//实际长度
int capacity;//可用长度
};
typedef struct Vector vector;
void initVector(vector * v){
v->array=NULL;
v->len=0;
v->capacity=0;
}
void destoryVector(vector * v){
if(v->array!=NULL){
free(v->array);
}
}
void push_backVector(vector * v,int value){
if(v->len < v->capacity){
v->array[v->len]=value;
v->len++;
}
else{
if(v->array==NULL){
v->array=(int*)malloc(sizeof(int));
v->capacity=1;
}
int *big_array=(int*)malloc(sizeof(int)*(v->capacity)*2);
memcpy(big_array,v->array,sizeof(int)*(v->capacity));
free(v->array);
v->array=big_array;
v->capacity*=2;
v->array[v->len]=value;
v->len++;
}
}
int pop_backVector(vector * v){
if(v->len==0){
printf("Error\n");
return 0;
}
if(v->len==1){
v->array=NULL;
v->len=0;
v->capacity=0;
}
else if(v->len-1==(v->capacity)/2){
int *small_array=(int*)malloc(sizeof(int)*(v->capacity)/2);
memcpy(small_array,v->array,v->len-1);
free(v->array);
v->array=small_array;
v->capacity/=2;
v->len--;
}
else v->len--;
return 0;
}
int size_Vector(vector * v){
return v->len;
}
int capacity_Vector(vector * v){
return v->capacity;
}
int* popindexVetor(vector * v , int p){
if(p>=v->len){
return NULL;
}
return v->array+p;
}
int main()
{
vector vt;
initVector(&vt);
for(int i=1;i<=10;i++){
push_backVector(&vt,i);
}
for(int i=1;i<=10;i++){
printf("element_%d->%d\n",i,vt.array[i-1]);
}
// int n=1;
// while(scanf("%d",&n)!=EOF){
printf("%d\n",popindexVetor(&vt,2));
// }
for(int i=0;i<10;i++){
pop_backVector(&vt);
for(int j=0;j<vt.len;j++){
printf("%d ",vt.array[j]);
}
printf("\n");
}
return 0;
}