Advertisement
Guest User

Untitled

a guest
Apr 13th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #ifndef LISTADT_H
  2. #define LISTADT_H
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. //typedef int ItemType;
  9. template <class ItemType>
  10. class NodeType
  11. {
  12. public:
  13. ItemType info;
  14. NodeType<ItemType> * nextPtr;
  15. };
  16.  
  17. template <class ItemType>
  18. class List
  19. {
  20. public:
  21. List();
  22. void makeEmpty();
  23. bool isEmpty();
  24. void goToStart();
  25. void goToNext();
  26. bool isAtEnd();
  27. ItemType CurrentItem();
  28. void insert(ItemType);
  29. void deleteCurrentItem();
  30. int currentPosition();
  31. private:
  32. NodeType<ItemType> * head;
  33. NodeType<ItemType> * current;
  34. NodeType<ItemType> * previous;
  35. int position;
  36. };
  37.  
  38.  
  39. template <class ItemType>
  40. List<ItemType>::List()
  41. {
  42. head = NULL, current = NULL, previous = NULL;
  43. position = 1;
  44. }
  45. template <class ItemType>
  46. void List<ItemType>::makeEmpty()
  47. {
  48. if (!isEmpty())
  49. {
  50. goToStart();
  51. while (!isAtEnd())
  52. {
  53. goToNext();
  54. delete previous;
  55. previous = current;
  56. }
  57. head = NULL;
  58. current = NULL;
  59. previous = NULL;
  60. position = 1;
  61. }
  62. }
  63. template <class ItemType>
  64. bool List<ItemType>::isEmpty()
  65. {
  66. if (head == NULL)
  67. return true;
  68. else
  69. return false;
  70. }
  71. template <class ItemType>
  72. void List<ItemType>::goToStart()
  73. {
  74. if (position == 1)
  75. return;
  76. else
  77. {
  78. current = head;
  79. previous = NULL;
  80. position = 1;
  81. }
  82. }
  83. template <class ItemType>
  84. void List<ItemType>::goToNext()
  85. {
  86. if (!isAtEnd())
  87. {
  88. if (current->nextPtr == NULL)
  89. {
  90. previous = current;
  91. current = NULL;
  92. position += 1;
  93. }
  94. else
  95. {
  96. previous = current;
  97. current = current->nextPtr;
  98. position += 1;
  99. }
  100. }
  101. }
  102. template <class ItemType>
  103. bool List<ItemType>::isAtEnd()
  104. {
  105. if (current == NULL)
  106. return true;
  107. else
  108. return false;
  109. }
  110. template <class ItemType>
  111. ItemType List<ItemType>::CurrentItem()
  112. {
  113. if (!isAtEnd())
  114. return current->info;
  115. }
  116. template <class ItemType>
  117. void List<ItemType>::insert(ItemType num)
  118. {
  119. NodeType<ItemType> * temp;
  120. temp = new NodeType<ItemType>;
  121. temp->info = num;
  122.  
  123. if (head == NULL)
  124. {
  125. temp->nextPtr = NULL;
  126. head = temp;
  127. current = head;
  128. }
  129. else if (position == 1)
  130. {
  131. head = temp;
  132. head->nextPtr = current;
  133. current = head;
  134. }
  135. else if (!isAtEnd() && current->nextPtr == NULL)
  136. {
  137. temp->nextPtr = current;
  138. current = temp;
  139. if (previous != NULL)
  140. {
  141. previous->nextPtr = current;
  142. }
  143. }
  144. else
  145. {
  146. previous->nextPtr = temp;
  147. temp->nextPtr = current;
  148. current = temp;
  149. }
  150. }
  151. template <class ItemType>
  152. void List<ItemType>::deleteCurrentItem()
  153. {
  154. if (head == NULL)
  155. {
  156. cout << "List is empty." << endl;
  157. }
  158. else
  159. {
  160. if (current == head)
  161. {
  162. head = head->nextPtr;
  163. current->nextPtr = 0;
  164. delete current;
  165. current = head;
  166. }
  167. else if (isAtEnd())
  168. {
  169. cout << "End of list cannot delete. Move the the start of list." << endl;
  170. }
  171. else if (current->nextPtr == NULL)
  172. {
  173. delete current;
  174. current = 0;
  175. previous->nextPtr = NULL;
  176. current = previous->nextPtr;
  177. }
  178. else
  179. {
  180. previous->nextPtr = current->nextPtr;
  181. current->nextPtr = 0;
  182. delete current;
  183. current = previous->nextPtr;
  184. }
  185. }
  186. }
  187. template <class ItemType>
  188. int List<ItemType>::currentPosition()
  189. {
  190. return position;
  191. }
  192.  
  193. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement