-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.c
More file actions
159 lines (147 loc) · 3.5 KB
/
Copy pathvector.c
File metadata and controls
159 lines (147 loc) · 3.5 KB
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
// C++ STL std::vector translates to a dynamic array
// () -> initialize
// push_back(), pop_back(), back(), insert(idx,value), erase(idx)
// capacity (memory representation or memory allocation in bytes), size (logical representation of elements)
// even if size is zero, capacity may not be zero.
// O(1) amoritzed TC for push_back
// vector needs to have my elements, so i need a array, size, capacity
#include<stdio.h>
#include<stdlib.h>
// Vector struct
typedef struct Vector{
int *data;
int size;
int capacity;
} Vector;
// vector<int>arr(10)
// Vector* arr = initVector(10)
Vector* initVector(int initialCapacity){
Vector* arr = malloc(sizeof(Vector));
arr->capacity = initialCapacity > 0? initialCapacity: 1;
arr->size = 0; // tells the logical size of the array and also points to the next index where a new element can be appended
arr->data = malloc(arr->capacity*sizeof(int));
return arr;
}
// push_back
// idx: 0 1 2
// ele: 1 2 3
// push_back(4)
// idx: 0 1 2 3
// ele: 1 2 3 4
void push_back(Vector* arr, int value){
if(arr->size == arr->capacity){
arr->capacity *= 2;
int *temp = realloc(arr->data, arr->capacity*sizeof(int));
if(temp==NULL){
printf("Memory allocation failed\n");
exit(1);
}
arr->data = temp;
}
//*(arr->data + arr->size) = value; same thing
arr->data[arr->size] = value;
arr->size++;
}
// pop_back
// idx: 0 1 2
// ele: 1 2 3
// pop_back()
// idx: 0 1
// ele: 1 2
void pop_back(Vector* arr){
if(arr->size==0) return;
arr->size--;
return;
}
// back
int back(Vector* arr){
if(arr->size == 0) return -1;
return arr->data[arr->size - 1];
}
// front
int front(Vector* arr){
if(arr->size == 0) return -1;
return arr->data[0];
}
// idx: 0 1 2 3 4
// ele: 1 2 3 4 5
// insert(2, 7)
// idx: 0 1 2 3 4 5
// ele: 1 2 7 3 4 5
void insert(Vector* arr, int index, int value){
// index needs to be valid
if(index < 0 || index > arr->size){
printf("Index out of bounds\n");
return;
}
if(index==arr->size){
push_back(arr, value);
return;
}
// insert means. insert at the given index. shift all the elements after the index to right by 1.
// making sure we have enough space
if(arr->size == arr->capacity){
arr->capacity*=2;
int *temp = realloc(arr->data, arr->capacity*sizeof(int));
if(temp==NULL){
printf("Memory allocation failed\n");
exit(1);
}
arr->data = temp;
}
// Shifting elements to the right of (index) by 1.
for(int i=arr->size;i>index;i--){
arr->data[i] = arr->data[i-1];
}
arr->data[index] = value;
arr->size++;
}
// erase(idx)
// idx: 0 1 2 3 4
// ele: 1 2 3 4 5
// erase(2)
// idx: 0 1 2 3
// ele: 1 2 4 5
void erase(Vector* arr, int index){
// index needs to be valid
if(index < 0 || index >= arr->size){
printf("Index out of bounds\n");
return;
}
if(index == arr->size-1){
pop_back(arr);
return;
}
for(int i=index;i<arr->size - 1;i++){
arr->data[i] = arr->data[i+1];
}
arr->size--;
}
void freeVector(Vector* arr){
if(arr==NULL) return;
free(arr->data);
free(arr);
}
void printVector(Vector* arr){
for(int i=0;i<arr->size;i++){
printf("Element at index: %d is: %d\n", i, arr->data[i]);
}
}
int main(){
Vector* arr = initVector(1);
push_back(arr, 1);
push_back(arr, 2);
push_back(arr, 3);
push_back(arr, 4);
push_back(arr, 5);
// 1 2 3 4 5
printVector(arr);
// insert (2,7)
// 1 2 7 3 4 5
insert(arr, 2, 7);
printVector(arr);
// erase (1)
// 1 2 7 3 4 5 -> 1 7 3 4 5
erase(arr, 1);
printVector(arr);
}