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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
#include <iostream>
template<class Elem>
class Mylist {
public:
class Iterator;
Mylist<Elem>() { } // Default Constructor
Mylist<Elem>(const std::initializer_list<Elem>& rhs) { // Constructor with initializers
for (const auto& r : rhs)
push_back(r);
}
Mylist(const Mylist& rhs) { // Copy constructor (deep copy)
for (const auto& r : rhs)
push_back(r);
}
Mylist& operator=(const Mylist& rhs) { // Copy assignment operator
if (*this != rhs) {
if (size() < rhs.size()) {
auto r = rhs.begin();
for (auto& it : *this) {
it = *r;
++r;
}
for (auto t = r; t != rhs.end(); ++t)
push_back(*r);
}
else if (size() > rhs.size()) {
auto it = begin();
for (auto r : rhs) { // replace the data
*it = r;
++it;
}
last = (--it).curr;
sz = rhs.size();
for (auto e = last->succ; e != nullptr;) { // delete the extra elements
auto d{ e };
e = e->succ;
delete d;
}
last->succ = nullptr;
}
else {
auto it = begin();
for (auto& r : rhs) {
*it = r;
++it;
}
}
}
return *this;
}
Mylist(Mylist&& rhs) { return std::move(rhs); } // move constructor
Mylist& operator=(Mylist&& rhs) { return std::move(rhs); } // move assignment operator
~Mylist<Elem>() { // Destructor
for (auto e = first; e != nullptr; ) {
auto d = e;
e = e->succ;
delete d;
}
}
size_t size() const { return sz; }
Iterator begin() const { return Iterator(first); } // iterator to the first element
Iterator end() const { // iterator to one beyond the last element
if (last) return Iterator(last->succ);
else return last;
}
Iterator insert(Iterator, const Elem&);
Iterator erase(Iterator); // remove p from the list
bool operator==(const Mylist<Elem> rhs) {
return (first == rhs.first && last == rhs.last && sz == rhs.sz);
}
bool operator!=(const Mylist<Elem> rhs) {
return !(this == &rhs);
}
Elem& operator[](size_t n) {
if (n <= 0 || n >= size())
throw std::out_of_range("out of range error! ");
auto it{ begin() };
while (n > 0) {
++it;
--n;
}
return *it;
}
const Elem& operator[](size_t n) const {
if (n <= 0 || n >= size())
throw std::out_of_range("out of range error");
auto it{ begin() };
while (n > 0) {
++it;
--n;
}
return *it;
}
void push_back(const Elem& v); // insert v at end
void push_front(const Elem& v); // insert v at front
void pop_back(); // remove the last element
void pop_front(); // remove the first element
private:
size_t sz{};
class Link;
protected:
Link* first{};
Link* last{};
};
//*****************************************
template<class Elem>
class Mylist<Elem>::Link {
public:
Link(Elem v, Link* p = nullptr, Link* s = nullptr) :
val(v), prev(p), succ(s) { }
Link* prev;
Link* succ;
Elem val;
};
//****************************************
template<class Elem>
class Mylist<Elem>::Iterator {
private:
Link* curr = nullptr;
friend class Mylist<Elem>; // Inside the friend class (Mylist), it'll be possible for the instances
// of the class Iterator to access private/protected data
public:
Iterator(Link* p) : curr(p) { }
Iterator& operator++() { curr = curr->succ; return *this; } // forward
Iterator& operator--() { curr = curr->prev; return *this; } // backward
Elem& operator*() { return curr->val; } // get value (dereference)
bool operator==(const Iterator& b) const { return curr == b.curr; }
bool operator!=(const Iterator& b) const { return curr != b.curr; }
};
//****************************************************************************
template<class Elem> // add v after p in the list
typename Mylist<Elem>::Iterator Mylist<Elem>::insert(Iterator p, const Elem& v) {
if (p == nullptr)
throw std::out_of_range("Out of range, no insertion occurred!");
if (p == first) {
push_front(v);
return p;
}
if (p.curr == last) {
push_back(v);
return p;
}
p.curr->succ->prev = new Link(v, p.curr, p.curr->succ);
p.curr->succ = p.curr->succ->prev;
++sz;
return p;
}
//******************************************************************************
template<class Elem> // remove p from the list
typename Mylist<Elem>::Iterator Mylist<Elem>::erase(Iterator p)
{
if (p == nullptr)
throw std::out_of_range("Out of range, no insertion occurred!");
if (p.curr == first) {
pop_front();
return Iterator(first);
}
if (p.curr == last) {
pop_back();
return Iterator(last);
}
p.curr->prev->succ = p.curr->succ;
p.curr->succ->prev = p.curr->prev;
delete p.curr;
--sz;
return 0;
}
//**************************************************************
template<class Elem>
void Mylist<Elem>::push_back(const Elem& v) // insert v at end
{
last = (first == nullptr) ? (first = new Link(v)) : (last->succ = new Link(v, last));
++sz;
}
//******************************************************
template<class Elem>
void Mylist<Elem>::push_front(const Elem& v)
{
first = (first == nullptr) ? (first = new Link(v)) : (first->prev = new Link(v, nullptr, first));
++sz;
}
//****************************************
template<class Elem> // remove the last element
void Mylist<Elem>::pop_back()
{
if (size() == 0)
throw std::out_of_range("pop_back from empty list!");
if (first == last)
delete first;
else {
last = last->prev;
delete last->succ;
//last->succ = nullptr;
}
}
//******************************************
template<class Elem> // remove the first element
void Mylist<Elem>::pop_front()
{
if (size() == 0)
throw std::out_of_range("pop_first from empty list!");
if (first == last)
delete first;
else {
first = first->succ;
delete first->prev;
first->prev = nullptr;
}
}
//**************************************************
template<class Iter>
Iter high(Iter first, Iter last)
// Return an iterator to the element in [first, last) that has the highest value
{
Iter high = first;
for (Iter p = first; p != last; ++p)
if (*p > *high) high = p;
return high;
}
//******************************************************
int main() try
{
Mylist<double> l1; // Defalut ctor called
l1.push_back(10);
l1.push_back(12);
l1.push_back(15);
l1.push_back(20);
l1[2] = 29;
std::cout << "l1: ";
for (auto l : l1)
std::cout << l << ' ';
std::cout << '\n';
Mylist<double> l2 = l1; // copy ctor called
l2.push_back(25);
std::cout << "l2: ";
for (auto l : l2)
std::cout << l << ' ';
std::cout << '\n';
Mylist<double> l3 = { 2.2, 3.3, 4.4, 5.5 }; // ctor with initializers called
std::cout << "l3: ";
for (auto l : l3)
std::cout << l << ' ';
std::cout << '\n';
l2 = l3; // copy assignment called
std::cout << "l2 after assignment with l3\nl2: ";
for (auto l : l2)
std::cout << l << ' ';
std::cout << '\n';
auto l4{ Mylist<double> { 1,2,3,4,5,6,7} }; // move ctor
std::cout << "l4: ";
for (auto l : l4)
std::cout << l << ' ';
std::cout << '\n';
l4.pop_front();
l4.push_front(11);
auto p{ l4.begin() };
++p;
l4.erase(p);
p = l4.begin();
++p;
l4.insert(p, 33);
l4.pop_back();
l4.push_back(77);
std::cout << "l4 after change: ";
for (auto l : l4)
std::cout << l << ' ';
std::cout << '\n';
auto it = high(l1.begin(), l1.end());
std::cout << "\nThe highest value of l1 is: " << *it << '\n';
it = high(l2.begin(), l2.end());
std::cout << "The highest value of l2 is: " << *it << '\n';
it = high(l3.begin(), l3.end());
std::cout << "The highest value of l3 is: " << *it << "\n";
it = high(l4.begin(), l4.end());
std::cout << "The highest value of l4 is: " << *it << "\n\n";
system("pause");
return 0;
}
catch (std::out_of_range& e) {
std::cerr << e.what() << '\n';
abort();
}
| |