-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
172 lines (132 loc) · 3.62 KB
/
linked_list.cpp
File metadata and controls
172 lines (132 loc) · 3.62 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
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
#include "print_iterable.hpp"
template <typename Ty> class List
{
struct Node
{
Ty value;
std::unique_ptr<Node> next_node;
Node(Ty &&value) : value(std::move(value)) {}
} *last = nullptr;
std::unique_ptr<Node> first;
public:
void append(Ty &&value)
{
last = ((last ? last->next_node : first) = std::make_unique<Node>(std::move(value))).get();
}
void iterate(std::function<void(Ty&)> yield_fn = [](Ty &el) {std::cout << el << '\n';})
{
for (Node *n = first.get(); n; n = n->next_node.get())
yield_fn(n->value);
}
// C++
class CppIterator
{
Node *node;
public:
CppIterator(Node *node) : node(node) {}
bool operator!=(CppIterator it) const {return node != it.node;}
auto &operator*() {return node->value;}
void operator++() {node = node->next_node.get();}
};
CppIterator begin() const {return CppIterator(first.get());}
CppIterator end () const {return CppIterator(nullptr);}
// D
class DRange
{
Node *node;
public:
DRange(Node *node) : node(node) {}
bool empty() {return node == nullptr;}
auto &front() {return node->value;}
void popFront() {node = node->next_node.get();}
};
DRange range() const {return DRange(first.get());}
// Python
class PythonIterator
{
Node *node;
public:
PythonIterator(Node *node) : node(node) {}
Ty &__next__()
{
if (node == nullptr) throw StopIteration();
Ty &result = node->value;
node = node->next_node.get();
return result;
}
};
auto __iter__() const {return PythonIterator(first.get());}
// Rust
class RustIterator
{
Node *node;
public:
RustIterator(Node *node) : node(node) {}
std::optional<Ty> next()
{
if (node == nullptr) return std::nullopt;
Ty &result = node->value;
node = node->next_node.get();
return result;
}
};
auto iter() const {return RustIterator(first.get());}
// Java
class JavaIterator
{
Node *node;
public:
JavaIterator(Node *node) : node(node) {}
bool hasNext() {return node != nullptr;}
Ty &next()
{
if (!hasNext()) throw NoSuchElementException();
Ty &result = node->value;
node = node->next_node.get();
return result;
}
};
auto iterator() const {return JavaIterator(first.get());}
// С#
class CsharpIterator
{
bool iteration_started = false;
Node *node;
public:
CsharpIterator(Node *node) : node(node) {}
bool MoveNext()
{
if (!iteration_started) {
iteration_started = true;
return node != nullptr;
}
node = node->next_node.get();
return node != nullptr;
}
Ty &Current() {return node->value;}
};
auto GetEnumerator() const {return CsharpIterator(first.get());}
// 11l
class Iterator11l
{
Node *node;
public:
Iterator11l(Node *node) : node(node) {}
Ty ¤t() {return node->value;}
bool advance() {node = node->next_node.get(); return node != nullptr;}
};
std::optional<Iterator11l> iter11l() const
{
if (!first)
return std::nullopt;
return Iterator11l(first.get());
}
};
int main()
{
List<int> list;
list.append(1);
list.append(3);
list.append(4);
print_iterable(list);
}