Advertisement
Guest User

.cpp

a guest
Dec 14th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. #include "Deque.h"
  2.  
  3. template <>
  4. Deque <const char * > ::DequeItem::DequeItem(const char * const& new_info) : next(nullptr), prev(nullptr)
  5. {
  6. info = new char[strlen(new_info) + 1];
  7. strcpy((char*)info, new_info);
  8. }
  9.  
  10. template <>
  11. Deque <const char * > ::DequeItem :: ~DequeItem()
  12. {
  13. delete[] info;
  14. }
  15.  
  16. template <typename InfoType>
  17. void Deque <InfoType> ::erase()
  18. {
  19. while (PopFront());
  20. front = rear = nullptr;
  21. }
  22.  
  23. template <typename InfoType>
  24. void Deque <InfoType> ::clone(const Deque <InfoType>& oldDeque)
  25. {
  26. size = 0;
  27. for (DequeItem *curr = oldDeque.front; curr != nullptr; curr = curr->next)
  28. PushBack(curr->info);
  29. }
  30.  
  31. template <typename InfoType>
  32. Deque<InfoType> ::Deque() : front(nullptr), rear(nullptr), size(0) {}
  33.  
  34. template <typename InfoType>
  35. Deque <InfoType> ::Deque(const Deque<InfoType>& oldDeque) : front(nullptr), rear(nullptr), size(0)
  36. {
  37. clone(oldDeque);
  38. }
  39.  
  40. template <typename InfoType>
  41. Deque <InfoType> :: ~Deque()
  42. {
  43. erase();
  44. }
  45.  
  46. template <typename InfoType>
  47. Deque<InfoType>& Deque<InfoType> :: operator = (const Deque <InfoType>& oldDeque)
  48. {
  49. if (this != &oldDeque)
  50. {
  51. erase();
  52. clone(oldDeque);
  53. }
  54. return *this;
  55. }
  56.  
  57. template <typename InfoType>
  58. const InfoType& Deque <InfoType> ::Front() const
  59. {
  60. if (size == 0)
  61. throw exception("Impossible to see the first element of the deque!");
  62. return front->info;
  63. }
  64.  
  65. template <typename InfoType>
  66. const InfoType& Deque <InfoType> ::Back() const
  67. {
  68. if (size == 0)
  69. throw exception("Impossible to see the last element of the deque!");
  70. return rear->info;
  71. }
  72.  
  73. template <typename InfoType>
  74. void Deque <InfoType> ::PushFront(const InfoType& info)
  75. {
  76. DequeItem *temp = new DequeItem(info);
  77. if (size == 0)
  78. {
  79. rear = temp;
  80. }
  81. else
  82. {
  83. temp->next = front;
  84. front->prev = temp;
  85. }
  86. front = temp;
  87. size++;
  88. }
  89.  
  90. template <typename InfoType>
  91. void Deque <InfoType> ::PushBack(const InfoType& info)
  92. {
  93. DequeItem *temp = new DequeItem(info);
  94. if (size == 0)
  95. {
  96. front = temp;
  97. }
  98. else
  99. {
  100. rear->next = temp;
  101. temp->prev = rear;
  102. }
  103. rear = temp;
  104. size++;
  105. }
  106.  
  107. template <typename InfoType>
  108. bool Deque <InfoType> ::PopFront()
  109. {
  110. if (size == 0)
  111. return false;
  112. DequeItem *temp;
  113. temp = front;
  114. front = front->next;
  115. if (front != nullptr)
  116. front->prev = nullptr;
  117. delete temp;
  118. size--;
  119. if (size == 0)
  120. front = rear = nullptr;
  121. return true;
  122. }
  123.  
  124. template <typename InfoType>
  125. bool Deque <InfoType> ::PopBack()
  126. {
  127. if (size == 0)
  128. return false;
  129. DequeItem *temp;
  130. temp = rear;
  131. rear = rear->prev;
  132. if (rear != nullptr)
  133. rear->next = nullptr;
  134. delete temp;
  135. size--;
  136. if (size == 0)
  137. rear = front = nullptr;
  138. return true;
  139. }
  140.  
  141. template <typename InfoType>
  142. size_t Deque <InfoType> ::Size() const
  143. {
  144. return size;
  145. }
  146.  
  147. template <typename InfoType>
  148. void Deque <InfoType> ::PrintDeque(ostream& str, const char* delimiter) const
  149. {
  150. for (DequeItem *curr = front; curr != nullptr; curr = curr->next)
  151. str << curr->info << (curr == rear ? " " : delimiter);
  152. }
  153.  
  154. template <typename InfoType>
  155. void Deque <InfoType> ::PrintDequeInverse(ostream& str, const char* delimiter) const
  156. {
  157. for (DequeItem *curr = rear; curr != nullptr; curr = curr->prev)
  158. str << curr->info << (curr == front ? " " : delimiter);
  159. }
  160.  
  161. template <>
  162. static bool Deque <const char*> ::defaultCompare(const char* const& a, const char* const& b)
  163. {
  164. return strcmp(a, b) < 0;
  165. }
  166.  
  167. template <typename InfoType>
  168. void Deque <InfoType> ::MergeLists(DequeItem * listA, DequeItem * listB, DequeItem *&result, compare comp)
  169. {
  170. if (listA == nullptr || listB == nullptr)
  171. {
  172. result = (listB == nullptr ? listA : listB);
  173. return;
  174. }
  175. DequeItem * endMerged;
  176. if (comp(listA->info, listB->info))
  177. {
  178. endMerged = listA;
  179. listA = listA->next;
  180. }
  181. else
  182. {
  183. endMerged = listB;
  184. listB = listB->next;
  185. }
  186. result = endMerged;
  187. while (listA != nullptr && listB != nullptr)
  188. {
  189. if (comp(listA->info, listB->info))
  190. {
  191. endMerged->next = listA;
  192. listA = listA->next;
  193. }
  194. else
  195. {
  196. endMerged->next = listB;
  197. listB = listB->next;
  198. }
  199. endMerged = endMerged->next;
  200. }
  201. while (listA != nullptr)
  202. {
  203. endMerged->next = listA;
  204. listA = listA->next;
  205. endMerged = endMerged->next;
  206. }
  207. while (listB != nullptr)
  208. {
  209. endMerged->next = listB;
  210. listB = listB->next;
  211. endMerged = endMerged->next;
  212. }
  213. DequeItem * prevItem = nullptr;
  214. for (DequeItem * curr = result; curr != nullptr; curr = curr->next)
  215. {
  216. curr->prev = prevItem;
  217. prevItem = curr;
  218. }
  219. }
  220.  
  221. template <typename InfoType>
  222. void Deque <InfoType> ::Sort(compare comp)
  223. {
  224. DequeItem * temp;
  225. list <DequeItem *> itemList, tempList;
  226. for (DequeItem * curr = front; curr != nullptr; )
  227. {
  228. temp = curr;
  229. curr = curr->next;
  230. temp->next = temp->prev = nullptr;
  231. itemList.push_back(temp);
  232. }
  233. while (itemList.size() > 1)
  234. {
  235. while (itemList.size() > 1)
  236. {
  237. DequeItem *first, *second, *result;
  238. first = itemList.front();
  239. itemList.pop_front();
  240. second = itemList.front();
  241. itemList.pop_front();
  242. MergeLists(first, second, result, comp);
  243. tempList.push_back(result);
  244. }
  245. if (itemList.size() == 1)
  246. {
  247. tempList.push_back(itemList.front());
  248. itemList.pop_front();
  249. }
  250. itemList.splice(itemList.begin(), tempList);
  251. }
  252. if (itemList.size() == 1)
  253. {
  254. front = itemList.front();
  255. for (DequeItem * curr = front; curr != nullptr; curr = curr->next)
  256. {
  257. rear = curr;
  258. }
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement