Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.75 KB | None | 0 0
  1. /***********************************************************************
  2. 2: Author: Matt Klein
  3. 3:
  4. 4: Filename: List.h
  5. 5:
  6. 6: Desc: Linked List Template and implementations
  7. 7:
  8. 8: Date Created: 11/17/2014
  9. 9: **********************************************************************/
  10. 10:
  11. 11: #ifndef LinkedList_List_h
  12. 12: #define LinkedList_List_h
  13. 13:
  14. 14: class BADINDEX {};
  15. 15: template <class T>
  16. 16: class ContainerIfc
  17. 17: {
  18. 18: public:
  19. 19: virtual ContainerIfc <T>& pushFront(T) =0;
  20. 20: virtual ContainerIfc <T>& pushBack(T) =0;
  21. 21: virtual ContainerIfc <T>& popFront(T&) throw (BADINDEX) =0;
  22. 22: virtual ContainerIfc <T>& popBack(T&) throw (BADINDEX) =0;
  23. 23: virtual int getSize() =0;
  24. 24: virtual bool isEmpty() =0;
  25. 25: virtual T front() throw (BADINDEX) =0;
  26. 26: virtual T back() throw (BADINDEX) =0;
  27. 27: virtual T& operator [](int) throw (BADINDEX) =0;
  28. 28: };
  29. 29: template <class T>
  30. 30: class node
  31. 31: {
  32. 32: public:
  33. 33: T data;
  34. 34: node<T> *next;
  35. 35: node(T e)
  36. 36: {
  37. 37: data = e;
  38. 38: next = NULL;
  39. 39: }
  40. 40: };
  41. 41: template <class T>
  42. 42: class List : public ContainerIfc <T>
  43. 43: {
  44. 44: public:
  45. 45: List();
  46. 46: ~List();
  47. 47: List(const List&);
  48. 48: List <T>& operator = (List&);
  49. 49: List <T>& pushFront(T);
  50. 50: List <T>& pushBack(T);
  51. 51: List <T>& popFront(T&) throw(BADINDEX);
  52. 52: List <T>& popBack(T&) throw(BADINDEX);
  53. 53: int getSize();
  54. 54: bool isEmpty();
  55. 55: T front() throw(BADINDEX);
  56. 56: T back() throw(BADINDEX);
  57. 57: T& operator [](int) throw(BADINDEX);
  58. 58: private:
  59. 59: node<T> *head;
  60. 60: };
  61. 61:
  62. 62: /**************************
  63. 63:
  64. 64: name:List()
  65. 65: description: class constructor
  66. 66: pre-condition: none
  67. 67: post-condition: class object created with currentSize = 0
  68. 68: , head points to NULL
  69. 69: return: nothing
  70. 70:
  71. 71: *************************/
  72. 72: template <class T>
  73. 73: List<T>::List()
  74. 74: {
  75. 75: head = NULL;
  76. 76: }
  77. 77:
  78. 78:
  79. 79:
  80. 80:
  81. 81:
  82. 82: /**************************
  83. 83:
  84. 84: name:~List()
  85. 85: description: class destructor
  86. 86: pre-condition: a List object exists
  87. 87: post-condition: all memory allocated to the List is
  88. 88: deleted
  89. 89: return: nothing
  90. 90:
  91. 91:
  92. 92: *************************/
  93. 93: template <class T>
  94. 94: List<T>::~List()
  95. 95: {
  96. 96: node<T> *curr = head;
  97. 97: while(head != NULL)
  98. 98: {
  99. 99: curr = head;
  100. 100: head = head->next;
  101. 101: delete curr;
  102. 102: }
  103. 103: curr = NULL;
  104. 104: }
  105. 105:
  106. 106:
  107. 107:
  108. 108:
  109. 109:
  110. 110: /**************************
  111. 111:
  112. 112: name: List(const List&)
  113. 113: description: class copy constructor
  114. 114: pre-condition: a List object exists
  115. 115: post-condition: a copy of the class object n is made
  116. 116: return: nothing
  117. 117:
  118. 118: *************************/
  119. 119: template <class T>
  120. 120: List<T>::List( const List& obj )
  121. 121: {
  122. 122:
  123. 123: head = NULL;
  124. 124: node<T> *temp = obj.head, *ptr = head;
  125. 125: if(temp != NULL)
  126. 126: {
  127. 127: while(temp != NULL)
  128. 128: {
  129. 129: pushBack( temp->data );
  130. 130: temp = temp->next;
  131. 131: }
  132. 132: }
  133. 133: delete temp;
  134. 134: }
  135. 135:
  136. 136:
  137. 137:
  138. 138:
  139. 139:
  140. 140: /**************************
  141. 141:
  142. 142: name: operator =
  143. 143: description: overloaded assignment operator
  144. 144: pre-condition: a object exists
  145. 145: post-condition: the object n is unchanged
  146. 146: and *this is an exact copy of n
  147. 147:
  148. 148:
  149. 149:
  150. 150: *************************/
  151. 151: template <class T>
  152. 152: List<T>& List<T>::operator = (List& obj)
  153. 153: {
  154. 154: if(this != &obj)
  155. 155: {
  156. 156: node<T> *curr = head;
  157. 157: while(head != NULL)
  158. 158: {
  159. 159: curr = head;
  160. 160: head = head->next;
  161. 161: delete curr;
  162. 162: }
  163. 163: curr = NULL;
  164. 164: }
  165. 165: head = NULL;
  166. 166: node<T> *temp = obj.head, *ptr = head;
  167. 167: if(temp != NULL)
  168. 168: {
  169. 169: while(temp != NULL)
  170. 170: {
  171. 171: pushBack( temp->data );
  172. 172: temp = temp->next;
  173. 173: }
  174. 174: }
  175. 175: delete temp;
  176. 176: return *this;
  177. 177: }
  178. 178:
  179. 179:
  180. 180:
  181. 181:
  182. 182:
  183. 183: /**************************
  184. 184:
  185. 185: name:pushFront(T)
  186. 186: description: store data element n in a New node
  187. 187: and adds the Node to the front
  188. 188: of a data structure (List)
  189. 189: pre-condition: a List object exists
  190. 190: post-condition: a new Node has been added
  191. 191: to the front of the List,
  192. 192: increment currentSize
  193. 193: return: a reference to self
  194. 194:
  195. 195: *************************/
  196. 196: template <class T>
  197. 197: List <T>& List<T>::pushFront(T val)
  198. 198: {
  199. 199: node<T> *temp = new node<T>(val);
  200. 200: temp->next = head;
  201. 201: head = temp;
  202. 202:
  203. 203: return *this;
  204. 204:
  205. 205: }
  206. 206:
  207. 207:
  208. 208:
  209. 209:
  210. 210:
  211. 211: /**************************
  212. 212:
  213. 213: name:pushBack(T)
  214. 214: description: store data element n in a New node
  215. 215: and adds the Node to the rear (end)
  216. 216: of a data structure (List)
  217. 217: pre-condition: a List object exists
  218. 218: post-condition: a new data element has been added
  219. 219: to the rear (end) of the List,
  220. 220: increment currentSize
  221. 221: return: a reference to self
  222. 222:
  223. 223: *************************/
  224. 224: template <class T>
  225. 225: List <T>& List<T>::pushBack(T val)
  226. 226: {
  227. 227: node<T> *temp = new node<T>(val);
  228. 228: if(head == NULL)
  229. 229: {
  230. 230: head = temp;
  231. 231: }
  232. 232: else
  233. 233: {
  234. 234: node<T> *ptr = head;
  235. 235: while(ptr->next != NULL)
  236. 236: {
  237. 237: ptr = ptr->next;
  238. 238: }
  239. 239: ptr->next = temp;
  240. 240: }
  241. 241: return *this;
  242. 242: }
  243. 243:
  244. 244:
  245. 245:
  246. 246:
  247. 247:
  248. 248: /**************************
  249. 249:
  250. 250: name:popFront(T)
  251. 251: description: removes a data element from the front
  252. 252: of the data structure (List)
  253. 253: pre-condition: a object exists
  254. 254: post-condition: a Node has been removed
  255. 255: from the front of the linked List
  256. 256: pointed to by head, decrement currentSize
  257. 257: error: if the List is empty
  258. 258: throw a BADINDEX exception
  259. 259: return: a reference to self
  260. 260:
  261. 261: *************************/
  262. 262: template <class T>
  263. 263: List <T>& List<T>::popFront(T& val) throw (BADINDEX)
  264. 264: {
  265. 265: if(isEmpty())
  266. 266: {
  267. 267: throw BADINDEX();
  268. 268: }
  269. 269: else if(head->next == NULL)
  270. 270: {
  271. 271: val = head->data;
  272. 272: head = NULL;
  273. 273: }
  274. 274: else
  275. 275: {
  276. 276: val = head->data;
  277. 277: node<T> *temp = head;
  278. 278: head = head->next;
  279. 279: temp->next = NULL;
  280. 280: delete temp;
  281. 281: }
  282. 282:
  283. 283:
  284. 284:
  285. 285: return *this;
  286. 286:
  287. 287: }
  288. 288:
  289. 289:
  290. 290:
  291. 291:
  292. 292:
  293. 293: /**************************
  294. 294:
  295. 295: name:popBack(T)
  296. 296: description: removes a data element from the rear (end)
  297. 297: of the data structure (List)
  298. 298: pre-condition: a object exists
  299. 299: post-condition: a Node has been removed
  300. 300: from the rear of the linked List
  301. 301: pointed to by head, decrement currentSize
  302. 302: error: if the List is empty
  303. 303: throw a BADINDEX exception
  304. 304: return: a reference to self
  305. 305:
  306. 306: *************************/
  307. 307: template <class T>
  308. 308: List <T>& List<T>::popBack(T& val) throw (BADINDEX)
  309. 309: {
  310. 310: if(isEmpty())
  311. 311: {
  312. 312: throw BADINDEX();
  313. 313: }
  314. 314: else if(head->next == NULL)
  315. 315: {
  316. 316: node<T> *ptr = head;
  317. 317: head = NULL;
  318. 318: val = ptr->data;
  319. 319: delete ptr;
  320. 320: }
  321. 321: else
  322. 322: {
  323. 323: node<T> *temp = head, *prev = head;
  324. 324:
  325. 325: while(temp->next != NULL)
  326. 326: {
  327. 327: prev = temp;
  328. 328: temp = temp->next;
  329. 329: }
  330. 330:
  331. 331: val = temp->data;
  332. 332: prev->next = NULL;
  333. 333: delete temp;
  334. 334: }
  335. 335:
  336. 336: return *this;
  337. 337:
  338. 338: }
  339. 339:
  340. 340:
  341. 341:
  342. 342:
  343. 343:
  344. 344: /**************************
  345. 345:
  346. 346: name:getSize()
  347. 347: description: returns the number of Nodes
  348. 348: currently in the linked List
  349. 349: pre-condition: a object exists
  350. 350: post-condition: the linked List is unchanged
  351. 351: return: an integer value representing
  352. 352: the number of elements in the List
  353. 353:
  354. 354:
  355. 355: *************************/
  356. 356: template <class T>
  357. 357: int List<T>::getSize()
  358. 358: {
  359. 359: int size = 0;
  360. 360: node<T> *a = head;
  361. 361:
  362. 362: while(a != NULL)
  363. 363: {
  364. 364: size++;
  365. 365: a = a->next;
  366. 366: }
  367. 367:
  368. 368: return size;
  369. 369: }
  370. 370:
  371. 371:
  372. 372:
  373. 373:
  374. 374:
  375. 375: /**************************
  376. 376:
  377. 377: name:isEmpty()
  378. 378: description: checks to see if head is empty
  379. 379: pre-condition: head
  380. 380: post-condition:head
  381. 381: return: whether its empty or not
  382. 382:
  383. 383: *************************/
  384. 384: template <class T>
  385. 385: bool List <T>::isEmpty()
  386. 386: {
  387. 387: bool check = false;
  388. 388:
  389. 389: if(getSize() == 0)
  390. 390: {
  391. 391: check = true;
  392. 392: }
  393. 393:
  394. 394: return check;
  395. 395: }
  396. 396:
  397. 397:
  398. 398:
  399. 399:
  400. 400:
  401. 401: /**************************
  402. 402:
  403. 403: name:front()
  404. 404: description: returns a copy of the first data item
  405. 405: in the linked List
  406. 406: pre-condition: a object exists
  407. 407: post-condition: the object is unchanged
  408. 408: error: throw BADINDEX if the
  409. 409: linked List is empty
  410. 410:
  411. 411: *************************/
  412. 412: template <class T>
  413. 413: T List<T>::front() throw (BADINDEX)
  414. 414: {
  415. 415: if(isEmpty())
  416. 416: {
  417. 417: throw BADINDEX();
  418. 418: }
  419. 419:
  420. 420: return head->data;
  421. 421: }
  422. 422:
  423. 423:
  424. 424:
  425. 425:
  426. 426:
  427. 427: /**************************
  428. 428:
  429. 429: name:back()
  430. 430: description: returns a copy of the last data item
  431. 431: in the linked List
  432. 432: pre-condition: a object exists
  433. 433: post-condition: the object is unchanged
  434. 434: error: throw BADINDEX if the
  435. 435: linked List is empty
  436. 436:
  437. 437: *************************/
  438. 438: template <class T>
  439. 439: T List<T>::back() throw (BADINDEX)
  440. 440: {
  441. 441: if(isEmpty())
  442. 442: {
  443. 443: throw BADINDEX();
  444. 444: }
  445. 445:
  446. 446: T back = NULL;
  447. 447:
  448. 448: if(head->next == NULL)
  449. 449: {
  450. 450: back = head->data;
  451. 451: }
  452. 452: else
  453. 453: {
  454. 454: node<T> *b = head;
  455. 455:
  456. 456: for(int i = 0; i < getSize() - 1; i++)
  457. 457: {
  458. 458: b = b->next;
  459. 459: }
  460. 460:
  461. 461: back = b->data;
  462. 462: }
  463. 463:
  464. 464: return back;
  465. 465:
  466. 466: }
  467. 467:
  468. 468:
  469. 469:
  470. 470:
  471. 471:
  472. 472: /**************************
  473. 473:
  474. 474: name:operator []
  475. 475: description: returns a reference to data element n
  476. 476: in the linked List
  477. 477: pre-condition: a object exists
  478. 478: post-condition: the object is unchanged
  479. 479: error: throw BADINDEX if n >= currentSize
  480. 480: or n < 0
  481. 481:
  482. 482: *************************/
  483. 483: template <class T>
  484. 484: T& List<T>::operator [](int val) throw (BADINDEX)
  485. 485: {
  486. 486: if(val < 0 || val >= getSize())
  487. 487: {
  488. 488: throw BADINDEX();
  489. 489: }
  490. 490:
  491. 491: node<T> *temp = head;
  492. 492:
  493. 493: for(int i = 0; i < val; i++)
  494. 494: {
  495. 495: temp = temp->next;
  496. 496: }
  497. 497:
  498. 498: return temp->data;
  499. 499: }
  500. 500:
  501. 501:
  502. 502: #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement