Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. #ifndef dll_h
  2. #define dll_h
  3. #include <iostream>
  4. using namespace std;
  5. template <class ItemType>
  6. struct NodeType
  7. {
  8. ItemType info;
  9. NodeType* next;
  10. NodeType* back;
  11. };
  12.  
  13.  
  14. template <class ItemType>
  15. class DoublyLinkedList
  16. {
  17. public:
  18. DoublyLinkedList(); // Class constructor.
  19. ~DoublyLinkedList(); // Class destructor.
  20.  
  21. ////////// implement these functions //////////
  22. DoublyLinkedList(DoublyLinkedList<ItemType>& );
  23. void InsertItem(ItemType item);
  24. void DeleteItem(ItemType item);
  25. void reverseDoublyLinkedList();
  26.  
  27.  
  28. void FindItem(NodeType<ItemType>* listData, ItemType item, NodeType<ItemType>*& location, bool& found);
  29. int LengthIs() const;
  30. void MakeEmpty();
  31. void RetrieveItem(ItemType& item, bool& found);
  32. void ResetList();
  33. void GetNextItem(ItemType& item);
  34.  
  35. private:
  36. NodeType<ItemType>* listData;
  37. int length;
  38. NodeType<ItemType>* currentPos;
  39. };
  40. #endif
  41.  
  42. #include "DLL.h"
  43. template<class ItemType>
  44. DoublyLinkedList<ItemType>::DoublyLinkedList()
  45. {
  46. listData = NULL;
  47. length =0;
  48. currentPos = NULL;
  49.  
  50. }
  51. template<class ItemType>
  52. void DoublyLinkedList<ItemType>::FindItem(NodeType<ItemType>* listData, ItemType item,
  53. NodeType<ItemType>*& location, bool& found)
  54. // Assumption: ItemType is a type for which the operators "<" and
  55. // "==" are defined-either an appropriate built-in type or a
  56. // class that overloads these operations.
  57. // Pre: List is not empty.
  58. // Post: If there is an element someItem whose key matches item's
  59. // key, then found = true; otherwise, found = false.
  60. // If found, location contains the address of someItem;
  61. // otherwise, location contains the address of the logical
  62. // successor of item.
  63. {
  64. bool moreToSearch = true;
  65.  
  66. location = listData;
  67. found = false;
  68. while (moreToSearch && !found)
  69. {
  70. if (item < location->info)
  71. moreToSearch = false;
  72. else if (item == location->info)
  73. found = true;
  74. else
  75. {
  76. location = location->next;
  77. moreToSearch = (location != NULL);
  78. }
  79. }
  80. }
  81.  
  82.  
  83. template <class ItemType>
  84. int DoublyLinkedList<ItemType>::LengthIs() const
  85. {
  86. return length;
  87. }
  88.  
  89. template <class ItemType>
  90. void DoublyLinkedList<ItemType>::MakeEmpty()
  91. // Post: List is empty; all items have been deallocated.
  92. {
  93. NodeType<ItemType>* tempPtr;
  94.  
  95. while (listData != NULL)
  96. {
  97. tempPtr = listData;
  98. listData = listData->next;
  99. delete tempPtr;
  100. }
  101. length = 0;
  102. }
  103.  
  104. template <class ItemType>
  105. void DoublyLinkedList<ItemType>::ResetList()
  106. {
  107. currentPos = NULL;
  108. }
  109.  
  110. template <class ItemType>
  111. void DoublyLinkedList<ItemType>::GetNextItem(ItemType& item)
  112. {
  113. if (currentPos == NULL)
  114. currentPos = listData;
  115. else
  116. currentPos = currentPos->next;
  117. item = currentPos->info;
  118. }
  119.  
  120. template <class ItemType>
  121. void DoublyLinkedList<ItemType>::RetrieveItem(ItemType& item,
  122. bool& found)
  123. {
  124. bool moreToSearch;
  125. NodeType<ItemType>* location;
  126.  
  127. location = listData;
  128. found = false;
  129. moreToSearch = (location != NULL);
  130.  
  131. while (moreToSearch && !found)
  132. {
  133. if (location->info < item)
  134. {
  135. location = location->next;
  136. moreToSearch = (location != NULL);
  137. }
  138. else if (item == location->info)
  139. {
  140. found = true;
  141. item = location->info;
  142. }
  143. else
  144. moreToSearch = false;
  145. }
  146. }
  147.  
  148. template <class ItemType>
  149. DoublyLinkedList<ItemType>:: ~DoublyLinkedList() // Class destructor.
  150. {
  151. MakeEmpty();
  152. }
  153.  
  154. template <class ItemType>
  155. void DoublyLinkedList<ItemType>::InsertItem(ItemType item)
  156. {
  157. NodeType<ItemType>* node = new NodeType<ItemType>;
  158. node->info = item;
  159. if(!length)
  160. {
  161. listData = node;
  162. node->next = NULL;
  163. node->back = NULL;
  164. length++;
  165. return;
  166. }
  167. NodeType<ItemType>* temp =listData;
  168. while(temp->next != NULL && node->info > temp->next->info)
  169. temp=temp->next;
  170. node->back = temp;
  171. node->next = temp->next;
  172. if(temp->next != NULL)
  173. node->next->back = node;
  174. temp->next = node;
  175. length++;
  176. }
  177.  
  178.  
  179. template <class ItemType>
  180. void DoublyLinkedList<ItemType>::DeleteItem(ItemType item)
  181. {
  182. NodeType<ItemType>* node = listData;
  183. if(node == NULL)
  184. return;
  185. while(node != NULL && node->info != item)
  186. node = node->next;
  187. if(node == NULL)
  188. return;
  189. if(node->back != NULL)
  190. node->back->next = node->next;
  191. if(node->next != NULL)
  192. node->next->back = node->back;
  193. delete node;
  194. length--;
  195. }
  196.  
  197. template <class ItemType>
  198. DoublyLinkedList<ItemType>::DoublyLinkedList(DoublyLinkedList<ItemType>& original)
  199. {
  200. length = original.length;
  201. if(original.listData == NULL)
  202. {
  203. listData = NULL;
  204. currentPos = NULL;
  205. return;
  206. }
  207. listData = new NodeType<ItemType>;
  208. NodeType<ItemType> *tempB, *tempN, *node = listData;
  209. NodeType<ItemType>* copy = original.listData;
  210. node->back = NULL;
  211. while(copy != NULL)
  212. {
  213. if(original.currentPos == copy)
  214. currentPos = node;
  215. node->info = copy->info;
  216. tempB = node;
  217. tempN = new NodeType<ItemType>;
  218. copy = copy->next;
  219. node->next = tempN;
  220. node=node->next;
  221. node->back = tempB;
  222. }
  223. node->next = NULL;
  224. }
  225.  
  226. template<class ItemType>
  227. void DoublyLinkedList<ItemType>::reverseDoublyLinkedList()
  228. {
  229. if(listData == NULL)
  230. return;
  231. if(listData->next == NULL)
  232. return;
  233. NodeType<ItemType> *node, *first, *newNode, *tempB, *tempN;
  234. node = listData;
  235. while(node->next != NULL)
  236. node = node->next;
  237. first = node;
  238. newNode = new NodeType<ItemType>;
  239. newNode->back = NULL;
  240. while(node != NULL);
  241. {
  242. if(currentPos == node)
  243. currentPos = newNode;
  244. newNode->info = node->info;
  245. tempB = newNode;
  246. tempN = new NodeType<ItemType>;
  247. node = node->back;
  248. newNode->next = tempN;
  249. newNode = newNode->next;
  250. newNode->back = tempB;
  251. }
  252. newNode->next = NULL;
  253. listData = first;
  254. }
  255.  
  256. #include <iostream>
  257. #include "dll.h"
  258. #include "dll.cpp"
  259.  
  260. int main()
  261. {
  262. DoublyLinkedList<int> s;
  263. s.InsertItem(3);
  264. s.InsertItem(4);
  265. s.InsertItem(1);
  266. s.DeleteItem(2);
  267. s.DeleteItem(4);
  268. cout<<"Length of s: "<<s.LengthIs()<<endl;
  269. DoublyLinkedList<int> t = s;
  270. cout<<"Length of t: "<<t.LengthIs()<<endl;
  271. s.InsertItem(5);
  272. s.InsertItem(8);
  273. s.InsertItem(10);
  274. s.InsertItem(1);
  275. int a;
  276. for(int i=0;i<s.LengthIs();i++)
  277. {
  278. s.GetNextItem(a);
  279. cout<<"Item #"<<i<<": "<<a<<endl;
  280. }
  281. for(int i=0;i<s.LengthIs();i++)
  282. {
  283. s.GetNextItem(a);
  284. cout<<"Item #"<<i<<": "<<a<<endl;
  285. }
  286. cout<<endl;
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement