Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. /***************************************************************
  2. Author: Dr. Daniel Spiegel, Updated by: Trisha Badlu
  3. Creation Date: 19 April 2017
  4. Due Date: 26 April 2017
  5. Assignment: #4
  6. Filename: LinkedList.h
  7. Course: CSC136 - 020
  8. Professor Name: Dr. Spiegel
  9. Purpose: The purpose of this file is to declare each
  10. prototype of the LinkedList class and to
  11. define an iterator.
  12. ***************************************************************/
  13. // File: LinkedList.h
  14. // LinkedList class with node and listIterator class
  15. // Recognizes Multiple Copies of Items During Insertion
  16. // Returns pointer to data if data found in list during insertion
  17. // does not insert item more than once
  18. // Find returns pointer to data if found.
  19.  
  20. #ifndef _LinkedList_
  21. #define _LinkedList_
  22. #include <assert.h>
  23. #include <iostream>
  24.  
  25. using namespace std;
  26.  
  27. // Need to prototype template classes if they are to be friends
  28. template <typename eltType> class LinkedList;
  29. template <typename eltType> class listItr;
  30.  
  31. template <typename eltType> class node
  32. {private:
  33. node(eltType info, node* link = NULL ) : data(info), next(link) {};
  34. eltType data;
  35. node* next;
  36. friend class LinkedList<eltType>;
  37. friend class listItr<eltType>;
  38. };
  39.  
  40. template <typename eltType> class LinkedList
  41. {
  42. public:
  43. /***************************************************************
  44. Function name: LinkedList (mutator)
  45. Description: Creates an empy LinkedList
  46. Parameters: none
  47. Return Value: none
  48. ***************************************************************/
  49. LinkedList();
  50. /***************************************************************
  51. Function name: LinkedList (mutator)
  52. Description: Creates a deep copy of a LinkedList
  53. Parameters: LinkedList& - cl (import/export)
  54. Return Value: none
  55. ***************************************************************/
  56. LinkedList(LinkedList& cl);
  57. /***************************************************************
  58. Function name: LinkedList (mutator)
  59. Description: Destroys a LinkedList
  60. Parameters: none
  61. Return Value: none
  62. ***************************************************************/
  63. ~LinkedList();
  64. /***************************************************************
  65. Function name: operator= (facilitator)
  66. Description: Assign another LinkedList to this LinkedList
  67. Parameters: LinkedList& - cl (import/export)
  68. Return Value: LinkedList - *this
  69. ***************************************************************/
  70. LinkedList& operator=(const LinkedList &cl);
  71. /***************************************************************
  72. Function name: empty (facilitator)
  73. Description: Checks if a LinkedList is empty
  74. Parameters: none
  75. Return Value: bool - true (if empty)
  76. false (if not empty)
  77. ***************************************************************/
  78. bool empty();
  79. /***************************************************************
  80. Function name: find (facilitator)
  81. Description: Checks if an object of eltType already exists in
  82. a LinkedList
  83. Parameters: const eltType& - elt (import/export)
  84. Return Value: eltType - &p->data (if found)
  85. NULL (if not found)
  86. ***************************************************************/
  87. eltType* find(const eltType &elt);
  88. /***************************************************************
  89. Function name: orderedInsert (mutator)
  90. Description: Inserts an object of eltType into a LinkedList
  91. alphanumerically
  92. Parameters: const eltType& - elt (import/export)
  93. Return Value: eltType - &p->data (if object already exists)
  94. NULL (if object is inserted)
  95. ***************************************************************/
  96. eltType *orderedInsert(const eltType &elt);
  97. /***************************************************************
  98. Function name: remove (mutator)
  99. Description: Removes an object of eltType from the LinkedList,
  100. assuming that the specified object already exists
  101. within the LinkedList
  102. Parameters: const eltType& - elt (import/export)
  103. Return Value: none
  104. ***************************************************************/
  105. void remove(const eltType &elt);
  106. /***************************************************************
  107. Function name: countNodesInList (facilitator)
  108. Description: Counts the amount of nodes that exist in a
  109. LinkedList
  110. Parameters: none
  111. Return Value: int - total amount of nodes starting at head
  112. ***************************************************************/
  113. // Quick example of recursion
  114. int countNodesInList() const;
  115.  
  116. private:
  117. // linked list pointer
  118. node<eltType>* head;
  119. /***************************************************************
  120. Function name: copy (mutator)
  121. Description: Gets a deep copy of a node
  122. Parameters: node<eltType>* - l (import/export)
  123. Return Value: node<eltType> - first
  124. ***************************************************************/
  125. node<eltType>* copy(node<eltType> *);
  126. /***************************************************************
  127. Function name: destroy (mutator)
  128. Description: Free nodes of a LinkedList
  129. Parameters: node<eltType>* - l (import/export)
  130. Return Value: none
  131. ***************************************************************/
  132. void destroy(node<eltType> *);
  133. /***************************************************************
  134. Function name: countNodes (facilitator)
  135. Description: Counts the amount of nodes that exist in a
  136. LinkedList
  137. Parameters: node<eltType>* - l (import/export)
  138. Return Value: int - total amount of nodes starting at head
  139. ***************************************************************/
  140. int countNodes(node<eltType> *) const;
  141. /*
  142. // Linked list to ostream
  143. friend ostream& operator<< <>(ostream&, LinkedList<eltType>);
  144. */
  145.  
  146. // Needed to use a list iterator
  147. friend class listItr<eltType>;
  148. };
  149.  
  150. template <typename eltType>
  151. /***************************************************************
  152. Function name: operator<< (facilitator)
  153. Description: Prints out a LinkedList
  154. Parameters: ostream& - os (import/export)
  155. const LinkedList<eltType> - &l (import/export)
  156. Return Value: ostream& - os
  157. ***************************************************************/
  158. ostream& operator<<(ostream &os,const LinkedList<eltType> &l);
  159.  
  160. // Set up an iterator;
  161. // an object that provides a pointer to a linked list (in this case)
  162. template <typename eltType> class listItr
  163. {
  164. public:
  165. /***************************************************************
  166. Function name: listItr (mutator)
  167. Description: Creates a list iterator
  168. Parameters: LinkedList<eltType>& - l
  169. Return Value: none
  170. ***************************************************************/
  171. listItr( LinkedList<eltType> &l);
  172. /***************************************************************
  173. Function name: listItr (mutator)
  174. Description: Creates from a const reference to a LinkedList
  175. Parameters: LinkedList<eltType>& - l
  176. Return Value: none
  177. ***************************************************************/
  178. listItr(const LinkedList<eltType> &l);
  179. /***************************************************************
  180. Function name: start (facilitator)
  181. Description: Points to first node in LinkedList
  182. Parameters: none
  183. Return Value: none
  184. ***************************************************************/
  185. void start();
  186. /***************************************************************
  187. Function name: more (facilitator)
  188. Description: Checks if the current node is NULL
  189. Parameters: none
  190. Return Value: bool - true (if node isn't NULL)
  191. false (if node is NULL)
  192. ***************************************************************/
  193. bool more() const;
  194. /***************************************************************
  195. Function name: next (facilitator)
  196. Description: Points to the next node in a LinkedList
  197. Parameters: none
  198. Return Value: none
  199. ***************************************************************/
  200. void next();
  201. /***************************************************************
  202. Function name: value (inspector)
  203. Description: Gets the value of the current node
  204. Parameters: none
  205. Return Value: eltType& - curr->data
  206. ***************************************************************/
  207. eltType &value();
  208. /***************************************************************
  209. Function name: value (inspector)
  210. Description: Gets the value of the current node
  211. Parameters: none
  212. Return Value: eltType& - curr->data
  213. ***************************************************************/
  214. const eltType &value() const;
  215. private:
  216. const LinkedList<eltType> &itr;
  217. node<eltType> *curr;
  218. };
  219.  
  220.  
  221. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement