List.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "List.h"
  2. #include <iostream>
  3. #include <fstream>
  4. List::List() :m_size(0)
  5. {
  6. /*ñôîðìèðîâàòü Head, Tail è m_size*/
  7. Head.pNext = &Tail;
  8. Tail.pPrev = &Head;
  9. }
  10. List::List(const List& other) :m_size(other.m_size)
  11. {
  12. Head.pNext = &Tail;
  13. Tail.pPrev = &Head;
  14. Node* pThis = Tail.pPrev;
  15. Node* pOther = other.Head.pNext;
  16. for (size_t i = 0; i < m_size; i++)
  17. {
  18. pThis = new Node(pThis, &pOther->m_Data);
  19. pOther = pOther->pNext;
  20. }
  21. }
  22. List::List(List&& other) :m_size(other.m_size)
  23. {
  24. if (m_size)
  25. {
  26. Head.pNext = other.Head.pNext;
  27. Tail.pPrev = other.Tail.pPrev;
  28. Head.pNext->pPrev = &Head;
  29. Tail.pPrev->pNext = &Tail;
  30. other.Head.pNext = &other.Tail;
  31. other.Tail.pPrev = &other.Head;
  32. other.m_size = 0;
  33. }
  34. else
  35. {
  36. Head.pNext = &Tail;
  37. Tail.pPrev = &Head;
  38. }
  39. }
  40. List::~List()
  41. {
  42. this->Remove_list();
  43. }
  44. void List::Ad_to(TO to, const Circle& circle)
  45. {
  46. Node* Prev = nullptr;
  47. switch (to)
  48. {
  49. case List::TO::to_head: Prev = &Head; break;
  50. case List::TO::to_tail: Prev = Tail.pPrev; break;
  51. //default: std::cout<<"Err" <<std::endl; break;
  52. }
  53. if (Prev)
  54. {
  55. new Node(Prev, &circle);
  56. m_size++;
  57. }
  58. }
  59. bool List::RemoveOne(const Circle& desired)
  60. {
  61. Node* p = Head.pNext;
  62. while (p != &Tail)
  63. {
  64. if (desired == p->m_Data)//find data
  65. {
  66. //if match - delete data, return true
  67. m_size--;
  68. delete p;
  69. return true;
  70. }
  71. //delete data, return true
  72. //else return false
  73. p = p->pNext;
  74. }
  75. return false;
  76. }
  77. int List::Remove_all_match(const Circle& desired)
  78. {
  79. static int deleted = 0;
  80. if (RemoveOne(desired))
  81. {
  82. deleted++;
  83. Remove_all_match(desired);
  84. }
  85. return deleted;
  86. }
  87. void List::Remove_list()
  88. {
  89. while (Head.pNext != &Tail)
  90. {
  91. delete Head.pNext;
  92. }
  93. m_size = 0;
  94. }
  95. void List::Swap(Node* replaceable, Node* movable)
  96. {
  97. if (replaceable == movable)return;
  98. if (replaceable->pNext == movable || replaceable->pPrev == movable)
  99. {
  100. Node* binding_tmp = replaceable->pPrev->pNext;
  101. replaceable->pPrev->pNext = movable->pNext->pPrev;
  102. movable->pNext->pPrev = binding_tmp;
  103. if (replaceable->pPrev == movable)
  104. {
  105. movable->pNext = replaceable->pNext;
  106. replaceable->pNext = movable;
  107. replaceable->pPrev = movable->pPrev;
  108. movable->pPrev = replaceable;
  109. return;
  110. }
  111. replaceable->pNext = movable->pNext;
  112. movable->pNext = replaceable;
  113. movable->pPrev = replaceable->pPrev;
  114. replaceable->pPrev = movable;
  115. return;
  116. }
  117. if (replaceable != &Tail &&
  118. replaceable != &Head &&
  119. movable != &Tail &&
  120. movable != &Head)
  121. {
  122. Node* tmpNext = replaceable->pNext;
  123. Node* tmpPrev = replaceable->pPrev;
  124. Node* binding_pNext = replaceable->pNext->pPrev;
  125. Node* binding_pPrev = replaceable->pPrev->pNext;
  126. replaceable->pNext->pPrev = movable->pNext->pPrev;
  127. replaceable->pPrev->pNext = movable->pPrev->pNext;
  128. movable->pNext->pPrev = binding_pNext;
  129. movable->pPrev->pNext = binding_pPrev;
  130. replaceable->pNext = movable->pNext;
  131. replaceable->pPrev = movable->pPrev;
  132. movable->pNext = tmpNext;
  133. movable->pPrev = tmpPrev;
  134. }
  135. }
  136. void List::Sort_list()
  137. {
  138. if (m_size <= 1) { return; }
  139. /*Node* left_node = Head.pNext;*/
  140. Node* min_node = Head.pNext;
  141. //åñëè ïëîùàäü âû÷èñëÿòü íå íàäî òî ìîæíî ñðàâíèâàòü ðàäèóñû
  142. /*double min = min_node->m_Data.Get_area();*/
  143. for (Node* left_node = Head.pNext; left_node->pNext != &Tail; left_node = left_node->pNext)
  144. {
  145. double min = left_node->m_Data.Get_area();
  146. bool fl = false;//ôëàæîê - áûëà ëè õîòÿ áû îäíà ïåðåñòàíîâêà
  147. for (Node* current = left_node->pNext; current != &Tail; current = current->pNext)
  148. {
  149. double cur = current->m_Data.Get_area();
  150. if (min > cur)
  151. {
  152. min = cur;
  153. min_node = current;
  154. fl = true;
  155. }
  156. }
  157. if (fl) { Swap(left_node, min_node); left_node = min_node; }
  158. /*else { break; }*/
  159. }
  160. }
  161. List& List::operator=(const List& other)
  162. {
  163. const Node* pOther_Head = &other.Head;
  164. Node* pOther = pOther_Head->pNext;
  165. Node* pThis = &Head;
  166. if (m_size == other.m_size || m_size > other.m_size)
  167. {
  168. while (pOther != &other.Tail)
  169. {
  170. pThis = pThis->pNext;
  171. pThis->m_Data = pOther->m_Data;
  172. pOther = pOther->pNext;
  173. }
  174. if (m_size > other.m_size)
  175. {
  176. pThis = pThis->pNext;
  177. while (pThis != &Tail)
  178. {
  179. Node* removable = pThis;
  180. pThis = pThis->pNext;
  181. delete removable;
  182. }
  183. m_size = other.m_size;
  184. }
  185. }
  186. if (m_size < other.m_size)
  187. {
  188. while (pThis->pNext != &Tail)
  189. {
  190. pThis = pThis->pNext;
  191. pThis->m_Data = pOther->m_Data;
  192. pOther = pOther->pNext;
  193. }
  194. while (pOther != &other.Tail)
  195. {
  196. pThis = new Node(pThis, &pOther->m_Data);
  197. pOther = pOther->pNext;
  198. }
  199. m_size = other.m_size;
  200. }
  201. return *this;
  202. }
  203. List& List::operator=(List&& other)
  204. {
  205. if (other.m_size)
  206. {
  207. if (m_size)
  208. {this->Remove_list();}
  209. Head.pNext = other.Head.pNext;
  210. Tail.pPrev = other.Tail.pPrev;
  211. Head.pNext->pPrev = &Head;
  212. Tail.pPrev->pNext = &Tail;
  213. m_size = other.m_size;
  214. other.Head.pNext = &other.Tail;
  215. other.Tail.pPrev = &other.Head;
  216. other.m_size = 0;
  217. }
  218. else
  219. {
  220. if (m_size != other.m_size)
  221. {
  222. this->Remove_list();
  223. }
  224. }
  225. return *this;
  226. }
  227. std::ostream& operator<<(std::ostream& os, const List& list)
  228. {
  229. os << "Size_of_list: " << list.m_size<<"\n";
  230. //Head.pNext - & 1 element
  231. //1 el.pNext - & Next el
  232. //if pNext ==ulptr - we are in Tait
  233. if (!list.m_size)
  234. {
  235. os << "Empty list";
  236. }
  237. else
  238. {
  239. Node* p = list.Head.pNext;
  240. while (p != &list.Tail)
  241. {
  242. os << *p << std::endl;// list.Head.pNext;
  243. p = p->pNext;
  244. }
  245. }
  246. os << "\n";
  247. return os;
  248. }
  249. List& operator>>(std::ifstream& file, List& list)
  250. {
  251. size_t tmp_size;
  252. Circle tmp_circ; //âðåìåííûé îáúåêò êðóæêà
  253. char buff[20];
  254. //file.getline(buff, 15) >> list.m_size; //why it is don't work?
  255. file >> buff >> tmp_size;
  256. for (size_t i = 0; i < tmp_size; i++)
  257. {
  258. file >> tmp_circ;
  259. list.Ad_to(List::TO::to_tail,tmp_circ);
  260. }
  261. return list;
  262. }