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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include <initializer_list>
#include <iostream>
template <typename T>
class List {
struct Node;
class It;
public:
List(std::initializer_list<T> ele) {
this->operator=(ele);
}
void InsertB(T ele) {
Node* node = new Node{ele};
if (this->size == 0) {
this->start = this->fin = node;
} else {
this->fin->next = node;
node->prev = this->fin;
this->fin = node;
}
++this->size;
}
void InsertF(T ele) {
Node* node = new Node{ele};
if (this->size == 0) {
this->start = this->fin = node;
} else {
node->next = this->start;
this->start->prev = node;
this->start = node;
}
++this->size;
}
void RemoveF() {
if (size == 0) {
return;
}
Node* temp = start->next;
delete start;
start = temp;
--size;
}
void RemoveB() {
if (size == 0) {
return;
}
if(start == NULL) {
return;
}
if(start->next == NULL) {
delete start;
start = NULL;
return;
}
Node* temp = start;
while (temp->next && temp->next->next != NULL) {
temp = temp->next;
}
delete temp->next;
temp->next = NULL;
fin = fin->prev;
--size;
}
List& operator=(std::initializer_list<T> ele) {
this->size = 0;
for (auto&& val : ele) this->InsertB(val);
return *this;
}
friend std::ostream& operator<<(std::ostream& out, const List& list) {
for (auto val = list.start; val; val = val->next) {
out << val->data;
if (val->next) out << ",";
}
return out;
}
template <class It>
List(It begin, It end) {
for (; begin != end; ++begin) this->InsertB(*begin);
}
It begin() {
return It(start);
}
It end() {
return It(NULL);
}
It InsertIt(It pos, const T& ele) {
if(pos == begin()) {
InsertF(ele);
return begin();
} else if(pos == end()) {
InsertB(ele);
return fin;
} else {
Node* temp = new Node{ele, pos.cur, pos.cur->prev};
pos.cur->prev->next = temp;
pos.cur->prev = temp;
++size;
return temp;
}
}
It InsertIt(It pos, std::size_t n, const T& val) {
for (int i = 0; i < n; i++) {
InsertIt(pos, val);
}
return pos;
}
template <typename InputIterator>
It InsertIt(It pos, InputIterator first, InputIterator last) {
InputIterator temp = first;
for (; first != last; first++) {
InsertIt(pos, *first);
}
return ++pos;
}
It InsertIt(It pos, std::initializer_list<T> list) {
InsertIt(pos, list.begin(), list.end());
return ++pos;
}
private:
struct Node {
T data;
Node* next = nullptr;
Node* prev = nullptr;
};
class It {
friend class List;
public:
// It& operator=(const It& that) {
// List<char> first(that.begin(), that.end());
// }
It& operator++() {
this->cur = cur->next;
return *this;
}
It operator++(int) {
It temp(*this);
this->operator++();
return temp;
}
T const *operator->() const { return &(**this); }
T *operator->() { return &(**this); }
T& operator*() const {return this->cur->data;}
bool operator!=(It val) const {
return !(this->operator==(val));
}
bool operator==(It val) const {
return this->cur == val.cur;
}
private:
Node* cur;
It (Node *ptr) : cur(ptr) {}
};
Node* start = nullptr;
Node* fin = nullptr;
std::size_t size = 0;
};
int main() {
List<char> test = {'H', 'e', 'l', 'l', 'o'};
std::cout << test << std::endl;
//List<char> test2 = {' ', 'W', 'o', 'r', 'l', 'd'};
auto iter = test.begin();
iter++;
iter++;
iter++;
iter++;
iter++;
test.InsertIt(iter, {' ', 'W', 'o', 'r', 'l', 'd'});
std::cout << test << std::endl;
}
| |