fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<typename T>
  5. void println(T data) {
  6. cout << data << endl;
  7. }
  8.  
  9. template<typename T>
  10. class List {
  11. public:
  12. class Item {
  13. protected:
  14. T data;
  15. Item* prev;
  16. Item* next;
  17. public:
  18. Item(T data, Item* prev, Item* next) {
  19. this->data = data;
  20. this->prev = prev;
  21. this->next = next;
  22. }
  23. T* getData() {
  24. return &data;
  25. }
  26. Item* getNext() {
  27. return next;
  28. }
  29. Item* getPrev() {
  30. return prev;
  31. }
  32. };
  33. private:
  34. class ExtendedItem : Item {
  35. public:
  36. void setPrev(Item* prev) {
  37. this->prev = prev;
  38. }
  39. void setNext(Item* next) {
  40. this->next = next;
  41. }
  42. void removeSelf() {
  43. if (next)
  44. ((ExtendedItem*)next)->setPrev(prev);
  45. if (prev)
  46. ((ExtendedItem*)prev)->setNext(next);
  47. }
  48. };
  49. Item* first;
  50. Item* last;
  51. public:
  52. List() {
  53. println("Default constructor called");
  54. first = NULL;
  55. last = NULL;
  56. }
  57. List(List<T>& other) {
  58. println("Copy constructor called");
  59. first = NULL;
  60. last = NULL;
  61. for (Item* cur = other.getFront(); cur != NULL; cur = cur->getNext()) {
  62. pushBack(*cur->getData());
  63. }
  64. }
  65. ~List() {
  66. println("Destructor called");
  67. while (first != NULL) {
  68. Item* next = first->getNext();
  69. // ((ExtendedItem*)first)->removeSelf();
  70. delete first;
  71. first = next;
  72. }
  73. }
  74. Item* pushFront(T data) {
  75. Item* newOne = new Item(data, NULL, first);
  76. if (first != NULL)
  77. ((ExtendedItem*)first)->setPrev(newOne);
  78. first = newOne;
  79. if (last == NULL)
  80. last = newOne;
  81. return newOne;
  82. }
  83. Item* pushBack(T data) {
  84. Item* newOne = new Item(data, last, NULL);
  85. if (last != NULL)
  86. ((ExtendedItem*)last)->setNext(newOne);
  87. last = newOne;
  88. if (first == NULL)
  89. first = newOne;
  90. return newOne;
  91. }
  92. Item* getFront() {
  93. return first;
  94. }
  95. Item* getBack() {
  96. return last;
  97. }
  98. void remove(Item* element) {
  99. if (element == NULL)
  100. return;
  101. ((ExtendedItem*)element)->removeSelf();
  102. if (element == first)
  103. first = first->getNext();
  104. if (element == last)
  105. last = last->getPrev();
  106. delete element;
  107. }
  108. template <typename C, bool Pred(T*, C)>
  109. Item* find(C context) {
  110. for (Item* cur = first; cur != NULL; cur = cur->getNext())
  111. if (Pred(cur->getData(), context))
  112. return cur;
  113. return NULL;
  114. }
  115. };
  116.  
  117. List<size_t>& newList(size_t a) {
  118. List<size_t> result;
  119. result.pushBack(a);
  120. return result;
  121. }
  122.  
  123. int main() {
  124. List<size_t> a = newList(1);
  125. for (List<size_t>::Item* cur = a.getFront(); cur != NULL; cur = cur->getNext())
  126. println(*cur->getData());
  127. return 0;
  128. }
Runtime error #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Default constructor called
Destructor called
Copy constructor called