This repository has been archived by the owner on Jul 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
162 lines (132 loc) · 3.83 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
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
#ifndef GRAYSET_VECTOR_H
#define GRAYSET_VECTOR_H
#include "header.h"
template <typename T>
class Node {
public:
T* _data;
Node* _next;
public:
Node() = delete;
explicit Node(const T& value) {
this->_data = new T(value);
this->_next = nullptr;
}
bool isend() const { return (this->_next == nullptr); }
const T& value() { return *this->_data; }
~Node() { free(this->_data); }
};
template <typename T>
class Vector {
private:
size_t _size;
Node<T>* _header;
public:
Vector(): _size(0), _header(nullptr) {}
size_t count() const { return this->_size; }
void append(const T& value) {
Node<T>* current = this->_header;
auto next = new Node<T>(value);
// For insertion as first element
if (current == nullptr) {
this->_header = next;
this->_size += 1;
return;
}
while (!current->isend())
current = current->_next;
current->_next = next;
this->_size += 1;
};
const T& get(size_t index) {
if (index >= this->_size || index < 0 || this->_header == nullptr)
throw std::out_of_range("Invalid index value.");
Node<T>* current = this->_header;
for (size_t _ = 0; _ < index; _++)
current = current->_next;
return *current->_data;
}
bool empty() const {
return (this->_size == 0);
}
bool exist(const T& value) const {
Node<T>* current = this->_header;
if (this->_header == nullptr)
return false;
while (!current->isend()) {
if (current->value() == value)
return true;
current = current->_next;
}
// Check for last item
if (current->value() == value)
return true;
return false;
};
// Remove one element
void remove(const T& value) {
Node<T>* current = this->_header;
if (this->_header == nullptr)
return;
size_t index = 0;
while (!current->isend()) {
if (current->value() == value) {
this->pop(index);
return;
}
current = current->_next;
index++;
}
}
T& index(size_t index) {
if (index >= this->_size || index < 0 || this->_header == nullptr)
throw std::out_of_range("Invalid index value.");
Node<T>* current = this->_header;
for (size_t _ = 0; _ < index; _++) {
current = current -> _next;
}
return *current->_data;
}
T pop(size_t index) {
if (index >= this->_size || index < 0 || this->_header == nullptr)
throw std::out_of_range("Invalid index value.");
Node<T>* next = nullptr;
Node<T>* previous = this->_header;
Node<T>* current = this->_header;
for (size_t _ = 0; _ < index; _++) {
previous = current;
current = current -> _next;
}
T value = *current->_data;
this->_size -= 1;
// For removing of first element
if (current == this->_header){
this->_header = current->_next;
delete current;
return value;
}
// For removing of last element
if (current->isend()) {
delete current;
previous->_next = nullptr;
return value;
}
next = current->_next;
delete current;
previous->_next = next;
return value;
};
~Vector() {
Node<T>* next;
Node<T>* current = this->_header;
if (current == nullptr)
return;
while (!current->isend()) {
next = current->_next;
delete current;
current = next;
}
delete current;
};
};
#endif //GRAYSET_VECTOR_H