-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotiv-unique.cpp
More file actions
49 lines (39 loc) · 1.1 KB
/
motiv-unique.cpp
File metadata and controls
49 lines (39 loc) · 1.1 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
#include <iostream>
#include <memory>
class IntList {
int value;
IntList* rest{nullptr};
// std::unique_ptr<IntList> rest;
public:
IntList(int value) : value(value) {}
// IntList(int value, std::unique_ptr<IntList>& rest) : value(value), rest(std::move(rest)) {}
IntList(int value, IntList* rest) : value(value), rest(rest) {}
void insert(int new_value) {
// rest = std::unique_ptr<IntList>(new IntList(value, rest));
// rest = std::make_unique<IntList>(value, rest); // C++14 only!
rest = new IntList(value, rest);
value = new_value;
}
int get_element() { return value; }
IntList* next() {
return rest;
// return rest.get();
}
// IntList(const IntList&) = delete;
// void operator=(const IntList&) = delete;
// ~IntList() {
// delete rest;
// }
};
int main() {
IntList l(1);
l.insert(3);
l.insert(17);
// IntList l2(l);
// IntList l2(2, l.next());
auto ptr = &l;
while (ptr != nullptr) {
std::cout << ptr->get_element() << '\n';
ptr = ptr->next();
}
}