Advertisement
Ermolaxe

Двунаправленный список(ШАБЛОН+ИСКЛЮЧЕНИЯ+ТЕСТЫ)

Jun 16th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #include "exception.cpp"
  2. template <class Item>
  3. class DoubleLinkedList
  4. {
  5. struct Element
  6. {
  7. Item inf;
  8. Element *next;
  9. Element *prev;
  10. Element(Item x) :inf(x), next(0), prev(0)
  11. {
  12. }
  13. };
  14. Element *head;
  15. Element *tail;
  16. int size;
  17. Element *Find(int index)
  18. {
  19. if ((index<1) || (index>size))
  20. {
  21. return NULL;
  22. }
  23. else
  24. {
  25. Element *cur=head;
  26. for (int i = 1; i < index; i++)
  27. {
  28. cur = cur->next;
  29. }
  30. return cur;
  31. }
  32. }
  33. public:
  34. //конструктор
  35. DoubleLinkedList() :head(0), tail(0), size(0)
  36. {
  37. }
  38. //деструктор
  39. ~DoubleLinkedList()
  40. {
  41. while (!Empty())
  42. {
  43. Remove(1);
  44. }
  45. }
  46. //проверяет список на пустоту
  47. bool Empty()
  48. {
  49. return head == 0;
  50. }
  51. //возвращает длину списка
  52. int GetLength()
  53. {
  54. return size;
  55. }
  56. //возвращает элемент на позиции index
  57. Item Get(int index)
  58. {
  59. if ((index<1) || (index>size))
  60. {
  61. throw DoubleListException("Exception: get - double-linked list error");
  62. }
  63. else
  64. {
  65. Element *r = Find(index);
  66. Item i = r->inf;
  67. return i;
  68. }
  69. }
  70. //вставить слева
  71. void InsertLeft(int index, Item data)
  72. {
  73. if ((index<1) || (index>size + 1))
  74. {
  75. throw DoubleListException("Exception: insert - double-linked list error");
  76. }
  77. else
  78. {
  79. Element *newPtr = new Element(data);
  80. size = GetLength() + 1;
  81. Element *cur = Find(index);
  82. if (cur == NULL)
  83. {
  84. head = newPtr;
  85. tail = newPtr;
  86. }
  87. else
  88. {
  89. newPtr->next = cur;
  90. newPtr->prev = cur->prev;
  91. cur->prev = newPtr;
  92. if (cur == head)
  93. {
  94. head = newPtr;
  95. }
  96. else
  97. {
  98. newPtr->prev->next = newPtr;
  99. }
  100. }
  101. }
  102. }
  103. //вставить справа
  104. void InsertRight(int index, Item data)
  105. {
  106. if ((index<1 && head != NULL) || (index>size + 1))
  107. {
  108. throw DoubleListException("Exception: insert - double-linked list error");
  109. }
  110. else
  111. {
  112. Element *newPtr = new Element(data);
  113. size = GetLength() + 1;
  114. Element *cur = Find(index);
  115. if (cur == NULL)
  116. {
  117. head = newPtr;
  118. tail = newPtr;
  119. }
  120. else
  121. {
  122. newPtr->next = cur->next;
  123. newPtr->prev = cur;
  124. cur->next = newPtr;
  125. if (cur == tail)
  126. {
  127. tail = newPtr;
  128. }
  129. else
  130. {
  131. newPtr->next->prev = newPtr;
  132. }
  133. }
  134. }
  135. }
  136. //удалить элемент на позиции index
  137. void Remove(int index)
  138. {
  139. if ((index<1) || (index>size))
  140. {
  141. throw DoubleListException("Exception: remove - double-linked list error");
  142. }
  143. else
  144. {
  145. Element *cur = Find(index);
  146. --size;
  147. if (size == 0)
  148. {
  149. head = NULL;
  150. tail = NULL;
  151. }
  152. else if (cur == head)
  153. {
  154. head = head->next;
  155. head->prev = NULL;
  156. }
  157. else if (cur == tail)
  158. {
  159. tail = tail->prev;
  160. tail->next = NULL;
  161. }
  162. else
  163. {
  164. cur->prev->next = cur->next;
  165. cur->next->prev = cur->prev;
  166. }
  167. cur->next = NULL;
  168. cur->prev = NULL;
  169. delete cur;
  170. }
  171. }
  172. //распечатать слева направо
  173. void PrintLeftToRight()
  174. {
  175. for (Element *cur = head; cur != NULL; cur = cur->next)
  176. {
  177. fout << cur->inf << " ";
  178. }
  179. fout << endl;
  180. }
  181. //распечатать справа налево
  182. void PrintRightToLeft()
  183. {
  184. for (Element *cur = tail; cur != NULL; cur = cur->prev)
  185. {
  186. fout << cur->inf << " ";
  187. }
  188. fout << endl;
  189. }
  190. };
  191.  
  192.  
  193. /////////////////////////////////////////////////////////////////////////////////
  194.  
  195.  
  196. #include "exception"
  197. #include "string"
  198. using namespace std;
  199. class DoubleListException : public exception
  200. {
  201. public:
  202. DoubleListException(const string & message = "") : exception(message.c_str())
  203. {
  204. }
  205. };
  206.  
  207.  
  208. /////////////////////////////////////////////////////////////////////////////////
  209.  
  210.  
  211. 1 1 1 4 6 6 0 5 1 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement