- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
#include <memory>
#include <list>
struct ListNode;
using List = std::unique_ptr<const ListNode>;
struct ListNode {
const int data;
const List next;
~ListNode()
{
if(!next)
return;
else {
std::list<ListNode*> nodes;
for(auto pn = next.get(); pn->next; pn = pn->next.get()) {
nodes.push_back(const_cast<ListNode*>(pn));
}
for(decltype(nodes)::reverse_iterator in = nodes.rbegin(); in != nodes.rend(); ++in) {
const_cast<List&>((*in)->next).reset();
}
}
}
};
List Cons(int head, List tail)
{
return List(new ListNode{head, std::move(tail)});
}
List Nil()
{
return List();
}
size_t len(const List & self)
{
if (!self) {
return 0;
}
return 1 + len(self->next);
}
#include <iostream>
void test(size_t n)
{
auto p = Nil();
for (size_t i = 0; i < n; ++i) {
auto x = std::move(p);
p = Cons(1, std::move(x));
}
std::cout << "done: " << std::endl;
}
int main()
{
test(131028);
}
Follow us!