Guest User

Untitled

a guest
May 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. //Specification for iterator class to be used with a linked lists.
  2. //Author: Judy Mullins
  3. #ifndef ListIterator_h
  4. #define ListIterator_h
  5. #include <ostream>
  6. #include <iostream>
  7. #include <cstddef> //for definition of NULL
  8. #include "list.h"
  9. using namespace std;
  10.  
  11.  
  12. template <class Type>
  13. class ListIterator
  14. {
  15. public:
  16. ListIterator(const List<Type> & L)
  17. {
  18. header = L.header;
  19. current = L.isEmpty() ? header : header->nextLink;
  20. }
  21. ~ListIterator() { cout << "calling iterator destructor" << endl;}
  22.  
  23. Type getEltAtPos(int i);// returns a copy of the element in the ith position if there is one;else returns error
  24. Type getEltAtCur(); // returns a copy of the element in the current position if there is one
  25. void insert(int x); // inserts at the beginning of the list; current is new element
  26. void insertCur(int x );// inserts x after current position, if current not NULL, and sets current to new element
  27. void remove(); // deletes the first element from a non-empty list
  28. bool remove(const Type & x);// deletes first occurrence of X if X found. Returns TRUE if success; FALSE otherwise.
  29. void removeCur(); // deletes the current element and sets current to the next element,
  30. //or null if there is no current element
  31. void advance(); // sets current to its successor
  32. void start(); // sets current to header node
  33. void setFirst(); // sets current to the first element of the list
  34. bool isFirst(); // returns TRUE if current is the first element of the list; else FALSE
  35. bool isLast(); // returns TRUE if current is the last element of the list; else FALSE
  36. bool hasNext(); //returns TRUE if current not NULL or header; FALSE otherwise.
  37.  
  38. bool find(const Type& x);//sets current position to first node containing x. Returns TRUE if x found, else returns FALSE.
  39. // current unchanged if x not found
  40. bool isFound(const Type & x)const; //returns TRUE if x is in list; FALSE otherwise
  41. void print(ostream& out); //prints contents of list to output stream
  42.  
  43. private:
  44. typename List<Type>::Node* current; //current position
  45. typename List<Type>::Node* header; //List header
  46. };
  47.  
  48.  
  49.  
  50. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  51. // L I S T I T E R A T O R I M P L E M E N T A T I O N
  52. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  53.  
  54. template <class Type>
  55. Type ListIterator<Type> :: getEltAtCur()
  56. {
  57. // returns a copy of the element in the current position if there is one
  58. if ( hasNext() )
  59. return current->info;
  60. else
  61. return 0; //error: current=header or current=NULL
  62. }
  63.  
  64. template <class Type>
  65. Type ListIterator<Type> :: getEltAtPos(int pos)
  66. {
  67. typename List<Type>::Node *temp = header;
  68.  
  69. if (current==header || current==NULL)
  70. {
  71. cout << "List is empty!!!" << endl;
  72. return 0;
  73. }
  74. for (int i=0; i < pos; i++)
  75. {
  76. temp = temp->nextLink;
  77. }
  78. return temp->info;
  79.  
  80. }
  81.  
  82.  
  83. template <class Type>
  84. void ListIterator<Type> :: insert(int elem)
  85. {
  86. // inserts at the beginning of the list; current is new element
  87. cout << "inserting at head: " << elem << endl;
  88. typename List<Type>::Node* newnode = new typename List<Type>::Node(elem,header->nextLink);
  89. header->nextLink = newnode;
  90. current = newnode;
  91. }
  92.  
  93.  
  94. template <class Type>
  95. void ListIterator<Type> :: insertCur(int elem)
  96. {
  97. //inserts x after current position, if current not NULL, and sets current to new element
  98. cout << "inserting after current: " << elem << endl;
  99. typename List<Type>::Node* newnode = new typename List<Type>::Node(elem,NULL);
  100. newnode->info = elem;
  101.  
  102. if(header->nextLink == NULL)
  103. {
  104. header->nextLink = newnode;
  105. }
  106. else
  107. {
  108. newnode->nextLink = current->nextLink;
  109. current->nextLink = newnode;
  110. }
  111. current = newnode;
  112. }
  113.  
  114.  
  115.  
  116. template <class Type>
  117. bool ListIterator<Type> :: remove(const Type & elem)
  118. {
  119. //deletes first occurrence of elem if elem found. Returns TRUE if success; FALSE otherwise.
  120. //current is reset to header if successful deletion.
  121. typename List<Type>::Node* temp = header->next;
  122. typename List<Type>::Node* prev = header;
  123.  
  124. while (temp != NULL && temp->elem != elem)
  125. {
  126. temp = temp->nextLink;
  127. prev = prev->nextLink;
  128. }
  129.  
  130. if(temp != NULL)
  131. {
  132. prev->nextLink = temp->nextLink;
  133. delete temp;
  134. current = header;
  135. count--;
  136. return TRUE;
  137. }
  138. else
  139. {
  140. return FALSE;
  141. }
  142.  
  143. }
  144.  
  145.  
  146.  
  147. template <class Type>
  148. void ListIterator<Type> :: remove()
  149.  
  150. { // deletes the first element from a non-empty list
  151.  
  152. cout << "removing first element" << endl;
  153. typename List<Type>::Node* temp;
  154. temp = header->nextLink->nextLink;
  155. delete header->nextLink;
  156. header->nextLink = temp;
  157. }
  158.  
  159.  
  160. template <class Type>
  161. void ListIterator<Type> :: removeCur()
  162. {
  163. // deletes the current element and sets current to the next element,
  164. //or null if there is no current element
  165. cout << "removing current element" << endl;
  166. typename List<Type>::Node* temp = header->nextLink;
  167.  
  168. while (temp->nextLink != current)
  169. {
  170. temp = temp->nextLink;
  171. }
  172.  
  173. current = current->nextLink;
  174. delete temp->nextLink;
  175. temp->nextLink = current;
  176. }
  177.  
  178.  
  179.  
  180. template <class Type>
  181. bool ListIterator<Type> :: hasNext()
  182. {
  183. //returns TRUE if current not NULL or header. Means current no longer points to a
  184. //node, or the list is empty
  185. if (current != NULL && current != header)
  186. return 1;
  187. else
  188. return 0;
  189. }
  190.  
  191.  
  192. template <class Type>
  193. void ListIterator<Type> :: advance()
  194. {
  195. current = current->nextLink;
  196. }
  197.  
  198.  
  199. template <class Type>
  200. void ListIterator<Type> :: start()
  201. {
  202. current = header;
  203. }
  204.  
  205.  
  206. template <class Type>
  207. void ListIterator<Type> :: setFirst()
  208. {
  209. // sets current to the first element of the list
  210. current = header->nextLink;
  211. }
  212.  
  213.  
  214. template <class Type>
  215. bool ListIterator<Type> :: isFirst()
  216. {
  217. return current == header->nextLink;
  218. }
  219.  
  220.  
  221. template <class Type>
  222. bool ListIterator<Type> :: isLast()
  223. {
  224. return current->nextLink == NULL;
  225. }
  226.  
  227.  
  228. template <class Type>
  229. bool ListIterator<Type> :: find(const Type& elem)
  230. {
  231. //sets current position to first node containing elem. Returns TRUE if elem found, else returns FALSE.
  232. //current unchanged if elem not found
  233. typename List<Type>::Node* temp = current; //save current in case elem not found
  234. setFirst(); //start iterator at beginning of list
  235.  
  236. while ( hasNext() )
  237. {
  238. if ( getEltAtCur() != elem )
  239. advance();
  240. else
  241. return TRUE; //current now points to elem
  242. }
  243. current = temp; //elem not found; set current back to original position
  244. return FALSE;
  245.  
  246. }
  247.  
  248.  
  249. template <class Type>
  250. bool ListIterator<Type> :: isFound(const Type & elem)const
  251. {
  252. typename List<Type>::Node* temp = header->nextLink;
  253.  
  254. while (temp != NULL && temp->elem != elem)
  255. temp = temp->nextLink;
  256.  
  257. return temp != NULL;
  258.  
  259. }
  260.  
  261.  
  262. template <class Type>
  263. void ListIterator<Type> :: print(ostream& out)
  264. {
  265. out << "List contents: "<< endl;
  266. typename List<Type>::Node* ptr = header->nextLink;
  267. if(header->nextLink == NULL)
  268. out <<"List is empty!!" <<endl;
  269. else
  270. while (ptr != NULL)
  271. {
  272. out << ptr->info << endl;
  273. ptr = ptr->nextLink;
  274. }
  275. return;
  276.  
  277. }
  278.  
  279. #endif
Add Comment
Please, Sign In to add comment