Advertisement
eogenio777

List

Dec 13th, 2019
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. template<typename T>
  5. class List
  6. {
  7. public:
  8. List();
  9. ~List();
  10. //удаление первого элемента в списке
  11. void pop_front();
  12. //добавление элемента в конец списка
  13. void push_back(T data_);
  14. //добавление с сортировкой
  15. void push( T data_);
  16. // очистить список
  17. //void push(T data_);
  18. void clear();
  19. // получить количество элементов в списке
  20. int GetSize() { return Size; }
  21. // перегруженный оператор []
  22. T& operator[](const int index);
  23. //добавление элемента в начало списка
  24. void push_front(T data_);
  25. //добавление элемента в список по указанному индексу
  26. void insert(T data_, int index);
  27. //удаление элемента в списке по указанному индексу
  28. void removeAt(int index);
  29. //удаление последнего элемента в списке
  30. void pop_back();
  31. friend void sort(List l);
  32. private:
  33. template<typename T>
  34. class Node
  35. {
  36. public:
  37. Node* pNext;
  38. T data_;
  39. Node(T data_ = T(), Node* pNext = nullptr)
  40. {
  41. this->data_ = data_;
  42. this->pNext = pNext;
  43. }
  44. };
  45. int Size;
  46. Node<T>* head;
  47. };
  48. template<typename T>
  49. List<T>::List()
  50. {
  51. Size = 0;
  52. head = nullptr;
  53. }
  54. template<typename T>
  55. List<T>::~List()
  56. {
  57. clear();
  58. }
  59. template<typename T>
  60. void List<T>::pop_front()
  61. {
  62. Node<T>* temp = head;
  63. head = head->pNext;
  64. delete temp;
  65. Size--;
  66. }
  67. template<typename T>
  68. void List<T>::push_back(T data_)
  69. {
  70. if (head == nullptr)
  71. {
  72. head = new Node<T>(data_);
  73. }
  74. else
  75. {
  76. Node<T>* current = this->head;
  77.  
  78. while (current->pNext != nullptr)
  79. {
  80. current = current->pNext;
  81. }
  82. current->pNext = new Node<T>(data_);
  83. }
  84. Size++;
  85. }
  86. //////////////////////////////////////PUSH_START
  87. template<typename T>
  88. void List<T>::push(T data_)
  89. {
  90. if (head == nullptr)
  91. {
  92. head = new Node<T>(data_);
  93. }
  94. else
  95. {
  96. Node<T>* current = this->head;
  97. while (current->pNext != nullptr && current->pNext->data_ < data_)
  98. current = current->pNext;
  99.  
  100. Node<T>* node = new Node<T>(data_);
  101. node->pNext = current->pNext;
  102. current->pNext = node;
  103. }
  104. Size++;
  105. }
  106. //////////////////////////////////////PUSH_END
  107. template<typename T>
  108. void List<T>::clear()
  109. {
  110. while (Size)
  111. {
  112. pop_front();
  113. }
  114. }
  115. template<typename T>
  116. T& List<T>::operator[](const int index)
  117. {
  118. int counter = 0;
  119.  
  120. Node<T>* current = this->head;
  121.  
  122. while (current != nullptr)
  123. {
  124. if (counter == index)
  125. {
  126. return current->data_;
  127. }
  128. current = current->pNext;
  129. counter++;
  130. }
  131. }
  132. template<typename T>
  133. void List<T>::push_front(T data_)
  134. {
  135. head = new Node<T>(data_, head);
  136. Size++;
  137. }
  138. template<typename T>
  139. void List<T>::insert(T data_, int index)
  140. {
  141.  
  142. if (index == 0)
  143. {
  144. push_front(data_);
  145. }
  146. else
  147. {
  148. Node<T>* previous = this->head;
  149.  
  150. for (int i = 0; i < index - 1; i++)
  151. {
  152. previous = previous->pNext;
  153. }
  154.  
  155. Node<T>* newNode = new Node<T>(data_, previous->pNext);
  156.  
  157. previous->pNext = newNode;
  158.  
  159. Size++;
  160. }
  161.  
  162.  
  163.  
  164.  
  165.  
  166. }
  167. template<typename T>
  168. void List<T>::removeAt(int index)
  169. {
  170. if (index == 0)
  171. {
  172. pop_front();
  173. }
  174. else
  175. {
  176. Node<T>* previous = this->head;
  177. for (int i = 0; i < index - 1; i++)
  178. {
  179. previous = previous->pNext;
  180. }
  181.  
  182.  
  183. Node<T>* toDelete = previous->pNext;
  184.  
  185. previous->pNext = toDelete->pNext;
  186.  
  187. delete toDelete;
  188.  
  189. Size--;
  190. }
  191.  
  192. }
  193. template<typename T>
  194. void List<T>::pop_back()
  195. {
  196. removeAt(Size - 1);
  197. }
  198. template <typename T>
  199. void sort(List <T> l)
  200. {
  201. for (int i = 1; i < l.GetSize; i++)
  202. {
  203. for (int j = i; j > 0 && l[j - 1] > l[j]; j--)
  204. {
  205. List a;
  206. a = l[j - 1];
  207. l[j-1] = l[j ];
  208. l[j] = a;
  209.  
  210. }
  211. }
  212. }
  213.  
  214.  
  215. int main()
  216. {
  217.  
  218. setlocale(LC_ALL, "ru");
  219. List<int> lst;
  220. lst.push_back('a');
  221. lst.push_back('b');
  222. lst.push_back('c');
  223. lst.push_back('d');
  224. for (int i = 0; i < lst.GetSize(); i++)
  225. {
  226. cout << lst[i] << endl;
  227. }
  228. cout << "...................................................." << endl;
  229. lst.clear();
  230. lst.push(1);
  231. lst.push(3);
  232. lst.push(6);
  233. lst.push(4);
  234. lst.push(5);
  235. for (int i = 0; i < lst.GetSize(); i++)
  236. {
  237. cout << lst[i] << endl;
  238. }
  239. cout << "...................................................." << endl;
  240. lst.clear();
  241. ////////
  242. //string a = "a", b = "b", c = "c", d = "d", f = "f";
  243. char a = 'a', b = 'b', c = 'c', d = 'd', f = 'f';
  244. lst.push(a);
  245. lst.push(b);
  246. lst.push(f);
  247. lst.push(d);
  248. lst.push(c);
  249. //cout << "5>3?: " << (lst[0] > lst[1]) << endl;
  250. for (int i = 0; i < lst.GetSize(); i++)
  251. {
  252. cout << lst[i] << endl;
  253. }
  254. cout << "size: " << lst.GetSize()<<endl;
  255. return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement