fork download
  1. #define indexTooLarge -1
  2. template <typename T>
  3. class LincedList
  4. {
  5. public:
  6. LincedList(){}
  7. template<typename... Types>
  8. LincedList(T value,Types... values) : LincedList(values...)
  9. {
  10. push_front(value);
  11. }
  12. LincedList(T value)
  13. {
  14. push_front(value);
  15. }
  16. ~LincedList()
  17. {
  18. clear();
  19. }
  20. void push_back(T value_a)
  21. {
  22. if(tail == nullptr)
  23. {
  24. tail = new Node(nullptr,value_a);
  25. head = tail;
  26. m_size++;
  27. return;
  28. }
  29. tail->next = new Node(nullptr,value_a);
  30. tail = tail->next;
  31. m_size++;
  32. }
  33. void push_front(T value_a)
  34. {
  35. if(head == nullptr)
  36. {
  37. head = new Node(nullptr,value_a);
  38. tail = head;
  39. m_size++;
  40. return;
  41. }
  42. head = new Node(head,value_a);
  43. m_size++;
  44. }
  45. void clear()
  46. {
  47. Node *buffer;
  48. for(int i = 0;i<m_size;i++)
  49. {
  50. buffer = head;
  51. head = head->next;
  52. delete buffer;
  53. }
  54. m_size = 0;
  55. }
  56. void remove(unsigned int index)
  57. {
  58. if(index >= m_size)
  59. throw indexTooLarge;
  60. Node *currectNode = head;
  61. for(int i = 0; i < index-1;i++)
  62. currectNode = currectNode->next;
  63. Node* buffer = currectNode->next;
  64. currectNode->next = currectNode->next->next;
  65. delete buffer;
  66. m_size--;
  67. }
  68. void remove(unsigned int index,unsigned int lenght)
  69. {
  70. if(index+lenght >= m_size)
  71. throw indexTooLarge;
  72. Node *currectNode = head;
  73. for(int i = 0; i < index-1; i++)
  74. currectNode = currectNode->next;
  75. Node* buffer = currectNode;
  76. currectNode = currectNode->next;
  77. for(int i = 0; i < lenght; i++ )
  78. {
  79. Node* buffer2 = currectNode;
  80. currectNode = currectNode->next;
  81. delete buffer2;
  82. }
  83. buffer->next = currectNode;
  84. m_size -= lenght;
  85. }
  86. T& operator[](unsigned const int &index)
  87. {
  88. if(index >= m_size)
  89. throw indexTooLarge;
  90. if(index == m_size-1)
  91. return tail->value;
  92. Node *currectNode = head;
  93. for(unsigned int i = 0; i < index;i++)
  94. currectNode = currectNode->next;
  95. return currectNode->value;
  96. }
  97. void operator=(const LincedList &C1)
  98. {
  99. head->value = C1.head->value;
  100. Node *currectNode = new Node(nullptr,C1[1]);
  101. Node *C1CurrectNode = C1[1];
  102. head->next = currectNode;
  103. for(int i = 2; i < m_size; i++)
  104. {
  105. C1CurrectNode = C1CurrectNode->next;
  106. currectNode->next = new Node(nullptr,C1CurrectNode->value);
  107. currectNode = currectNode->next;
  108. }
  109. tail->value = C1.tail->value;
  110. }
  111. unsigned int size()
  112. {
  113. return m_size;
  114. }
  115. private:
  116. struct Node
  117. {
  118. Node* next;
  119. T value;
  120. Node(Node* next_a, T value_a) : next(next_a) , value(value_a)
  121. {}
  122. };
  123. unsigned int m_size = 0;
  124. Node* head = nullptr;
  125. Node* tail = nullptr;
  126. };
  127.  
  128. #include <iostream>
  129. int main()
  130. {
  131. LincedList<int> lx(0,1,2,3);
  132. LincedList<int> ll(lx);
  133. std::cout << ll[1] << std::endl; //writes to console 1
  134. ll.remove(1); //removes the element at index 1
  135. std::cout << ll[1] << std::endl; //writes to console 2
  136. ll.push_back(4);//adds to the end 4
  137. ll.push_front(5);//adds 5 to the beginning
  138. std::cout << ll.size() << std::endl; //writes to console 5
  139. ll.remove(1,2); //remove all 2 to 3 elements
  140. std::cout << ll[1] << std::endl ; //writes to console 3
  141. return 0;
  142. }
Runtime error #stdin #stdout #stderr 0s 4412KB
stdin
Standard input is empty
stdout
1
2
5
3
stderr
free(): double free detected in tcache 2