-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
79 lines (64 loc) · 1.4 KB
/
vector.h
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
//
// Created by 倉澤 一詩
//
#ifndef MSLH_VECTOR_H
#define MSLH_VECTOR_H
template <typename T>
class Vector{
private:
struct Container{
int index{0};
T value;
Container* next_ptr;
Container(int _index, T& _value):index(_index),value(_value){
next_ptr = nullptr;
}
virtual ~Container(){
delete next_ptr;
}
};
int index{0};
Container *start;
public:
Vector(){
start = nullptr;
}
virtual ~Vector(){
delete start;
}
void push_back(T _val){
if(!start) {
start = new Container(0,_val);
}else{
Container *ptr=start;
while(ptr){
if(!ptr->next_ptr){
ptr->next_ptr = new Container(index,_val);
break;
}
ptr = ptr->next_ptr;
}
}
index += 1;
}
T get_value(int idx){
Container *ptr=start;
while(ptr->index < idx){
if (!ptr->next_ptr)break;
else ptr = ptr->next_ptr;
}
return ptr->value;
}
T& at(int idx){
Container *ptr=start;
while(ptr->index < idx){
if (!ptr->next_ptr)break;
else ptr = ptr->next_ptr;
}
return ptr->value;
}
int size(){
return index;
}
};
#endif //MSLH_VECTOR_H