Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class ListNotValid {};
  8. class AccessToTheEmptyList {};
  9. class OutOfRange {};
  10.  
  11.  
  12. template <class T>
  13. class List
  14. {
  15. private:
  16. struct Node
  17. {
  18. T info;
  19. Node* next;
  20. Node* prev;
  21. };
  22. Node *head,
  23. *tail;
  24.  
  25.  
  26. public:
  27.  
  28. List()
  29. {
  30. head = NULL;
  31. tail = NULL;
  32. }
  33.  
  34.  
  35.  
  36. int size()
  37. {
  38. int count = 0;
  39. Node* p = head;
  40. while(p != NULL)
  41. {
  42. count++;
  43. p = p->next;
  44. }
  45. return count;
  46. }
  47.  
  48.  
  49.  
  50. void push_front(T info)
  51. {
  52. Node* add = new Node;
  53. add->next = NULL;
  54. add->prev = NULL;
  55. add->info = info;
  56.  
  57.  
  58. if (head == NULL)
  59. {
  60. head = add;
  61. tail = add;
  62. }
  63. else
  64. {
  65. add->next = (head);
  66. (head)->prev = add;
  67. (head) = add;
  68. }
  69.  
  70.  
  71. }
  72.  
  73.  
  74. void push_back(T info)
  75. {
  76.  
  77. if (head == NULL)
  78. {
  79.  
  80. push_front(info);
  81. }
  82. else
  83. {
  84. (tail)->next = new Node;
  85. (tail)->next->prev = tail;
  86. tail = (tail)->next;
  87. (tail)->next = NULL;
  88. }
  89.  
  90. (tail)->info = info;
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98. void printAll()
  99. {
  100.  
  101.  
  102. if (head==NULL)
  103. {
  104. cout << "Spisok pust";
  105. return;
  106. }
  107.  
  108.  
  109. Node* p = head;
  110.  
  111. while (p != NULL)
  112. {
  113. cout << p->info << " ";
  114. p = p->next;
  115. }
  116. cout << endl;
  117. }
  118.  
  119.  
  120. void printReverseAll()
  121. {
  122. if (tail==NULL)
  123. {
  124. cout << "Spisok pust";
  125. return;
  126. }
  127.  
  128.  
  129. Node* p = tail;
  130. while (p != NULL)
  131. {
  132.  
  133. cout << p->info << ' ';
  134. p = p -> prev;
  135.  
  136. }
  137. cout <<endl;
  138. }
  139.  
  140.  
  141. void erase(int position)
  142. {
  143.  
  144. if(head == NULL) return;
  145.  
  146. Node* p = head;
  147. int cnt = 0;
  148.  
  149. while((p != NULL) && (cnt != position))
  150. {
  151. p = p->next;
  152. cnt++;
  153. }
  154.  
  155. if(p == NULL)
  156. {
  157. cout<<"del elem not found"<<endl;
  158. return;
  159. }
  160.  
  161. if(p == head)
  162. {
  163.  
  164. head = p->next;
  165.  
  166. if (head != NULL)
  167. {
  168. (head)->prev = NULL;
  169. }
  170. else
  171. {
  172. tail = NULL;
  173. }
  174. delete p;
  175. }
  176. else if (p == tail)
  177. {
  178. tail=(tail)->prev;
  179.  
  180. if (tail != NULL)
  181. {
  182. (tail)->next = NULL;
  183. }
  184. else
  185. {
  186. head = NULL;
  187. }
  188.  
  189. delete p;
  190. }
  191. else
  192. {
  193. Node* tmpi = head;
  194.  
  195. while (tmpi->next != p)
  196. {
  197. tmpi = tmpi->next;
  198. }
  199.  
  200. tmpi->next = p->next;
  201.  
  202. if (p->next != NULL)
  203. {
  204. p->next->prev = tmpi;
  205. }
  206. delete p;
  207. }
  208.  
  209.  
  210. }
  211.  
  212. T operator[](int i)
  213. {
  214. if(i<0)
  215. {
  216. throw OutOfRange();
  217. }
  218.  
  219. if(head == NULL)
  220. {
  221. throw OutOfRange();
  222.  
  223. }
  224. int count = 0;
  225. Node* p = head;
  226. while((p != NULL) && (i != count))
  227. {
  228.  
  229. count++;
  230. p = p->next;
  231. }
  232.  
  233. if(p == NULL)
  234. {
  235. throw OutOfRange();
  236. return NULL;
  237. }
  238. return p->info;
  239. }
  240.  
  241.  
  242. T front()
  243. {
  244. if(head == NULL)
  245. {
  246. //cout <<"no front element"<<endl;
  247.  
  248. throw AccessToTheEmptyList();
  249.  
  250.  
  251. }
  252. return head->info;
  253. }
  254.  
  255. T back()
  256. {
  257. if(tail == NULL)
  258. {
  259. throw AccessToTheEmptyList();
  260.  
  261. }
  262. return tail->info;
  263. }
  264.  
  265.  
  266. void clear()
  267. {
  268. while(size() != 0)
  269. {
  270. erase(0);
  271. }
  272. }
  273.  
  274. void add(List<T>& list2)
  275. {
  276. for(int i = 0;i<list2.size();i++)
  277. {
  278. push_back(list2[i]);
  279. }
  280.  
  281. }
  282.  
  283.  
  284. };
  285.  
  286.  
  287.  
  288.  
  289. int main()
  290.  
  291. try {
  292.  
  293. List<int> l;
  294.  
  295. for (int i = 0; i < 10; i++)
  296. {
  297. l.push_back(i);
  298. }
  299.  
  300. cout << "Numbers from 0 to 9: ";
  301. l.printAll();
  302. cout << "Numbers from 9 to 0: ";
  303. l.printReverseAll();
  304.  
  305. cout << "Numbers from 0 to 9: ";
  306. for (int i = 0; i < l.size(); i++)
  307. {//l.size() - count of elements in list
  308. cout << l[i] << " ";
  309. }
  310. cout << endl;
  311.  
  312. l.erase(9);//
  313. l.erase(0);
  314.  
  315. cout << "Numbers from 1 to 8: ";
  316. l.printAll();
  317. cout << "Numbers from 8 to 1: ";
  318. l.printReverseAll();
  319.  
  320. l.erase(2);
  321. l.erase(2);
  322. l.erase(2);
  323.  
  324. cout << "List after erasing third element three times: ";
  325. l.printAll();
  326.  
  327. cout << "First element: " << l.front() << endl;//1
  328. cout << "Last element: " << l.back() << endl;//8
  329.  
  330. l.clear();
  331.  
  332. try
  333. {
  334. cout << l.front() << endl;//throw AccessToTheEmptyList();
  335. }
  336. catch (AccessToTheEmptyList)
  337. {
  338. cout << "Exception test #1 passed." << endl;
  339. }
  340.  
  341. try
  342. {
  343. cout << l.back() << endl;//throw AccessToTheEmptyList();
  344. }
  345. catch (AccessToTheEmptyList)
  346. {
  347. cout << "Exception test #2 passed." << endl;
  348. }
  349.  
  350. try
  351. {
  352. cout << l[5] << endl;//throw OutOfRange();
  353. }
  354. catch (OutOfRange)
  355. {
  356. cout << "Exception test #3 passed." << endl;
  357. }
  358.  
  359. try
  360. {
  361. cout << l[-1] << endl;//throw OutOfRange();
  362. }
  363. catch (OutOfRange)
  364. {
  365. cout << "Exception test #4 passed." << endl;
  366. }
  367.  
  368. l.push_front(10);
  369. try
  370. {
  371. cout << l[1] << endl;//throw OutOfRange();
  372. }
  373. catch (OutOfRange) {
  374. cout << "Exception test #5 passed." << endl;
  375. }
  376.  
  377. for (int i = 0; i < 9; i++)
  378. {
  379. l.push_front(9-i);
  380. }
  381.  
  382. try
  383. {
  384. cout << l[10] << endl;//throw OutOfRange();
  385. }
  386. catch (OutOfRange)
  387. {
  388. cout << "Exception test #6 passed." << endl;
  389. }
  390.  
  391. cout << "Numbers from 1 to 10: ";
  392. l.printAll();
  393. l.clear();
  394.  
  395. l.push_back(1); l.push_back(2); l.push_back(3);
  396.  
  397. List<int> l2;
  398. l2.push_back(-1); l2.push_back(-2); l2.push_back(-3);
  399.  
  400. l.add(l2);
  401.  
  402. l.printAll();
  403. l.printReverseAll();
  404.  
  405. l2.add(l);
  406. l.add(l2);
  407. l2.clear();
  408.  
  409. l.add(l2);
  410.  
  411. l.printAll();
  412. l.printReverseAll();
  413.  
  414. cin.get();
  415. return 0;
  416. }
  417. catch (OutOfRange) {
  418. cout << "Testing fail by \"OutOfRange\"\n";
  419. cin.get();
  420. }
  421. catch (AccessToTheEmptyList) {
  422. cout << "Testing fail by \"AccessToTheEmptyList\"\n";
  423. cin.get();
  424. }
  425. catch (ListNotValid) {
  426. cout << "Testing fail by \"ListNotValid\"\n";
  427. cin.get();
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement