fork(4) download
  1. #include <memory>
  2.  
  3. struct ListNode;
  4. using List = std::unique_ptr<const ListNode>;
  5.  
  6. struct ListNode {
  7. const int data;
  8. const List next;
  9. };
  10.  
  11. List Cons(int head, List tail)
  12. {
  13. return List(new ListNode{head, std::move(tail)});
  14. }
  15.  
  16. List Nil()
  17. {
  18. return List();
  19. }
  20.  
  21. size_t len(const List & self)
  22. {
  23. if (!self) {
  24. return 0;
  25. }
  26. return 1 + len(self->next);
  27. }
  28.  
  29. #include <iostream>
  30.  
  31. void test(size_t n)
  32. {
  33. auto p = Nil();
  34. for (size_t i = 0; i < n; ++i) {
  35. auto x = std::move(p);
  36. p = Cons(1, std::move(x));
  37. }
  38. std::cout << "done: " << len(p) << std::endl;
  39. }
  40.  
  41. int main()
  42. {
  43. test(131028);
  44. }
Success #stdin #stdout 0.01s 6364KB
stdin
Standard input is empty
stdout
done: 131028