Posted By

DrPepper on 09/22/11


Tagged


Versions (?)

[CISP 430] sequence.TEMPLATE


 / Published in: C++
 

This version is complete version

  1. // This is the implementation file
  2.  
  3.  
  4. /*********************************************************************************************
  5.  
  6. USE TEMPLATE FUNCTION AND SHORT CUT WORKS !!
  7.  
  8. PTEMLATE FUNCTION MANIPULATE ALMOST EVERYTHING FOR US !!
  9.  
  10. **********************************************************************************************/
  11.  
  12. #include <iostream>
  13. #include <iomanip>
  14. #include <cassert>
  15. //#include "node2.h"
  16. #include "sequence4.h"
  17.  
  18. using namespace std;
  19.  
  20. namespace CISP430_A3
  21. {
  22. // TEMPLATE CLASS PROVIDED:
  23. // sequence<Item> (part of the namespace CISP430_A3)
  24. // This is a container class for a sequence of items,
  25. // where each List may have a designated item called the current item).
  26. // The template parameter <value_type> is the data type of the items
  27. // in the List. It may be any of the C++ built-in types (int, char, etc.),
  28. // or a class with a default constructor, an assignment operator,
  29. // and a copy constructor.
  30. //
  31. // TYPEDEFS and MEMBER CONSTANTS for the sequence class:
  32. // typename sequence<Item>::value_type
  33. // Thisis the data type of the items in the sequence. It
  34. // may be any of the C++ built-in types (int, char, etc.), or a class with a
  35. // default constructor, an assignment operator, and a copy constructor.
  36. //
  37. // typename sequence<Item>::size_type
  38. // This is the data type of any variable that keeps track of
  39. // how many items are in a sequence.
  40. //
  41. // CONSTRUCTOR for the sequence<Item> class:
  42.  
  43. template <class Item>
  44. sequence<Item>::sequence( )
  45. // Postcondition: The sequence has been initialized as an empty sequence.
  46. {
  47. head_ptr = NULL; precursor = NULL;
  48. tail_ptr = NULL; cursor = NULL; many_nodes = 0;
  49. }
  50.  
  51. //COPY CONSTRACTOR
  52. template <class Item>
  53. sequence<Item>::sequence(const sequence& source)
  54. { operator =(source); }
  55.  
  56. // DESTRACTOR
  57. template <class Item>
  58. sequence<Item>::~sequence( )
  59. {
  60. list_clear(head_ptr);
  61. many_nodes = NULL;
  62. }
  63.  
  64.  
  65. // MODIFICATION MEMBER FUNCTIONS for the sequence<Item> class:
  66. // START ALSO GOOD
  67. template <class Item>
  68. void sequence<Item>::start( )
  69. // Postcondition: The first item on the sequence becomes the current item
  70. // (but if the sequence is empty, then there is no current item).
  71. {
  72.  
  73. cursor = head_ptr;
  74. precursor = NULL;
  75. }
  76.  
  77. template <class Item>
  78. void sequence<Item>::advance( )
  79. // Precondition: is_item returns true.
  80. // Postcondition: If the current item was already the last item in the
  81. // sequence, then there is no longer any current item. Otherwise, the new
  82. // current item is the item immediately after the original current item.
  83. {
  84. if(!is_item())
  85. return;
  86. precursor = cursor;
  87.  
  88. if(cursor == tail_ptr)
  89. { cursor = NULL; }
  90. else
  91. { cursor = cursor->link(); }
  92. }
  93.  
  94.  
  95. // INSERT SEMMS RIGHT SO FAR
  96. template <class Item>
  97. void sequence<Item>::insert(const value_type& entry)
  98. // Postcondition: A new copy of entry has been inserted in the sequence
  99. // before the current item. If there was no current item, then the new entry
  100. // has been inserted at the front of the sequence. In either case, the newly
  101. // inserted item is now the current item of the sequence.
  102.  
  103. // YOU NEED TO USE list_copy() TO COPY ENTIRE LIST TO NEW PLACE WHEN YOU INSERT NEW ITEM IN THE MIDDLE OF SEQUENCE !!
  104. // WHICH MEANS YOU NEED TO CREATE FO LOOP PART HERE.
  105.  
  106. // BE CAREFUL !! THE PARAMETER RECEVE ENTRY AS VALUE TYPE NOT ITEM
  107.  
  108. {
  109. node<Item> *target_ptr = new node<Item>(entry);
  110. if((cursor == NULL) && (head_ptr != NULL))
  111. {
  112. target_ptr->set_link(head_ptr);
  113. cursor = target_ptr;
  114. precursor = NULL;
  115. head_ptr = target_ptr;
  116. }
  117. else if((cursor != NULL) && (precursor!= NULL))
  118. {
  119. precursor->set_link(target_ptr);
  120. target_ptr->set_link(cursor);
  121. }
  122. else if((cursor != NULL) && (cursor == head_ptr))
  123. {
  124. target_ptr->set_link(cursor);
  125. head_ptr = target_ptr;
  126. precursor = NULL;
  127. head_ptr = target_ptr;
  128. }
  129. else
  130. {
  131. tail_ptr = target_ptr;
  132. head_ptr = tail_ptr;
  133. }
  134. cursor = target_ptr;
  135. many_nodes++;
  136. }
  137.  
  138.  
  139.  
  140. template <class Item>
  141. void sequence<Item>::attach(const value_type& entry)
  142. // Postcondition: A new copy of entry has been inserted in the sequence after
  143. // the current item. If there was no current item, then the new entry has
  144. // been attached to the end of the sequence. In either case, the newly
  145. // inserted item is now the current item of the sequence.
  146. {
  147. node<Item> *target_tpr = new node<Item>(entry);
  148.  
  149. if((cursor == NULL) && (tail_ptr != NULL))
  150. {
  151. tail_ptr->set_link(target_tpr);
  152. list_head_insert(tail_ptr,target_tpr->data());
  153. }
  154. else if(((cursor != NULL) && (cursor == tail_ptr))
  155. ||((cursor != NULL) && (cursor == head_ptr) && (cursor == tail_ptr)))
  156. {
  157. precursor = cursor;
  158. cursor->set_link(target_tpr);
  159. tail_ptr = target_tpr;
  160. cursor = target_tpr;
  161. many_nodes++;
  162. }
  163. else if(((cursor != NULL) && (cursor == head_ptr))
  164. ||((cursor != tail_ptr) && (cursor != head_ptr)))
  165. {
  166. list_head_insert(precursor,cursor->data());
  167. target_tpr->set_link(cursor->link());
  168. cursor->set_link(target_tpr);
  169. many_nodes++;
  170. }
  171. else
  172. {
  173. tail_ptr = target_tpr;
  174. head_ptr = tail_ptr;
  175. many_nodes++;
  176. }
  177. cursor =target_tpr;
  178. }
  179.  
  180. template <class Item>
  181. void sequence<Item>::remove_current( )
  182. // Precondition: is_item returns true.
  183. // Postcondition: The current item has been removed from the sequence, and
  184. // the item after this (if there is one) is now the new current item.
  185. {
  186. if(!is_item())
  187. return;
  188.  
  189. if((cursor != tail_ptr) && (cursor != head_ptr)){
  190. precursor->set_link(cursor->link());
  191. list_head_remove(cursor);
  192. cursor = precursor->link();
  193. }
  194. else if((cursor != tail_ptr) && (cursor == head_ptr))
  195. {
  196. head_ptr = cursor->link();
  197. list_head_remove(cursor);
  198. cursor = head_ptr;
  199.  
  200. }
  201. else if((cursor == tail_ptr) && (precursor != NULL))
  202. {
  203. precursor->set_link(NULL);
  204. list_head_remove(cursor);
  205. cursor = NULL;
  206. tail_ptr = precursor;
  207. }
  208. else if((cursor == tail_ptr) && (cursor == head_ptr))
  209. {
  210. list_head_remove(cursor);
  211. head_ptr = NULL;
  212. tail_ptr = NULL;
  213. cursor = NULL;
  214. }
  215.  
  216. many_nodes--;
  217.  
  218. }
  219.  
  220. // CONSTANT MEMBER FUNCTIONS for the sequence<Item> class:
  221.  
  222. template <class Item>
  223. typename sequence<Item>::value_type sequence<Item>::current( ) const
  224. // Precondition: is_item( ) returns true.
  225. // Postcondition: The item returned is the current item in the sequence.
  226. {
  227. if(is_item())
  228. { return cursor->data( ); }
  229. else
  230. return 0;
  231. }
  232.  
  233. template <class Item>
  234. void sequence<Item>::operator =(const sequence& source)
  235. {
  236. value_type counter=0;
  237. cursor =source.cursor;
  238. tail_ptr = source.tail_ptr;
  239. while((cursor != tail_ptr)&&(cursor!=NULL))
  240. { advance(); counter++; }
  241. list_copy(source.head_ptr,head_ptr,tail_ptr);
  242. many_nodes = source.many_nodes;
  243. start();
  244. if(source.cursor == NULL)
  245. cursor = NULL;
  246. else
  247. {
  248. for(value_type counter2 =1;counter2<(source.many_nodes-counter); counter2++)
  249. advance();
  250. }
  251. }
  252. }// namespace end

Report this snippet  

You need to login to post a comment.