Advertisement
Guest User

Untitled

a guest
May 27th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. #ifndef LIST_H
  2. #define LIST_H
  3.  
  4.  
  5. #pragma once
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include "Node.h"
  9. using namespace std;
  10.  
  11.  
  12. template <class Type>
  13.  
  14. class List
  15. {
  16. private:
  17.  
  18.  
  19. class Node
  20. {
  21. public:
  22. // Public attributes - Why are the attributes public?
  23. Type data; // The data in the node
  24. Node* next; // Pointer to next node
  25.  
  26. // Constructors and destructor
  27.  
  28.  
  29. Node::Node()
  30. {
  31. //data = 0; //disable because we can't know if its an int
  32. next = NULL;
  33. }
  34.  
  35.  
  36. Node::Node(Type theData)
  37. {
  38. data = theData;
  39. next = NULL;
  40. }
  41.  
  42.  
  43. Node::Node(Type theData, Node* theNextNode)
  44. {
  45. data = theData;
  46. next = theNextNode;
  47. }
  48.  
  49.  
  50. Node::~Node()
  51. {
  52. }
  53.  
  54. }; // end Node
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. Node<Type> *head; //Pointer to the first node in the list
  62. int size; //Records the number of nodes in the list
  63.  
  64. public:
  65.  
  66. List(); //default constructor
  67. List(const List& lst); //copy constructor
  68. ~List();//destructor
  69. List& operator=(const List& list);
  70. Type& operator[](int index) ;
  71. const Type& operator[](int index) const;
  72. Type getItem(int index) const;
  73. // PRE:
  74. // POST: A new node containing the given data is inserted at the front
  75. // of the list
  76. // PARAM: data is the data to be stored
  77. void add(Type data);
  78.  
  79. // PRE:
  80. // POST: A new node containing the given data is inserted at the given
  81. // position in the list
  82. // PARAM: pos specifies the (index) position to insert the new node
  83. // data is the data to be stored
  84. void insertAt(int pos, Type data);
  85.  
  86. // PRE:
  87. // POST: The first incidence of the given data is removed from the list.
  88. // Returns true if data is found (and removed), false otherwise
  89. // PARAM: data specifies the data to be removed from the list
  90. bool remove(Type data );
  91.  
  92. // PRE:
  93. // POST: Empties the list, freeing up dynamically allocated memory
  94. // PARAM:
  95. void removeAll();
  96.  
  97. /* For Testing Purposes */
  98. // PRE:
  99. // POST: Prints the contents of the list to the screen, in order
  100. // PARAM:
  101. void printList();
  102. void copyList(const List& list);
  103. int getSize() const;
  104.  
  105.  
  106.  
  107. };
  108.  
  109.  
  110.  
  111.  
  112.  
  113. // Default constructor
  114. template <class Type>
  115. List<Type>::List()
  116. {
  117. head = NULL;
  118. size = 0;
  119. }
  120.  
  121.  
  122. template <class Type>
  123. List<Type>::List(const List& lst)
  124. {
  125. copyList(lst);
  126. }
  127.  
  128.  
  129.  
  130. template <class Type>
  131. List<Type>::~List()
  132. {
  133. removeAll();
  134. }
  135.  
  136. template <class Type>
  137. List<Type>& List<Type>::operator=(const List<Type>& list)
  138. {
  139. if (this != &list)
  140. {
  141. removeAll();
  142. copyList(list);
  143. }
  144. return *this;
  145. }
  146.  
  147.  
  148. template<class Type>
  149. void List<Type>::copyList(const List<Type> &lst)
  150. {
  151.  
  152. if (lst.head == NULL)
  153. {
  154. head = NULL;
  155. size = 0;
  156. }
  157. else
  158. {
  159.  
  160. head = new Node<Type>;
  161. head->data = lst.head->data;
  162. /* Now copy the rest of the list. To do this we'll need to create two
  163. * temporary pointers, one to go through the old list, one node at a
  164. * time, and one to keep pace in the new list, creating new nodes. */
  165. Node<Type> *pNewNode = head;
  166. Node<Type> *pOldNode = lst.head->next;
  167. // Repeat until the entire list is copied
  168. while (pOldNode != NULL)
  169. {
  170. pNewNode->next = new Node<Type>;
  171. pNewNode = pNewNode->next;
  172. pNewNode->data = pOldNode->data;;
  173. pOldNode = pOldNode->next;
  174. }
  175. pNewNode->next = NULL;
  176. size = lst.size;
  177. }
  178. }
  179.  
  180. template<class Type>
  181. int List<Type>::getSize() const
  182. {
  183. return size;
  184. }
  185.  
  186. template<class Type>
  187. Type& List<Type>::operator[](int index)
  188. {
  189.  
  190. if(getSize()>=0 && index<=size-1)
  191. {
  192. int counter=0;
  193. Node<Type> *p=head;
  194.  
  195. while( !(counter==index) )
  196. {
  197. p=p->next;
  198. counter++;
  199. }
  200.  
  201. return p->data;
  202. }
  203.  
  204. throw std::invalid_argument("wrong index");
  205. }
  206.  
  207.  
  208. template<class Type>
  209. const Type& List<Type>::operator[](int index) const
  210. {
  211.  
  212. if(getSize()>=0 && index<=size-1)
  213. {
  214. int counter=0;
  215. Node<Type> *p=head;
  216.  
  217. while( !(counter==index) )
  218. {
  219. p=p->next;
  220. counter++;
  221. }
  222.  
  223. return p->data;
  224. }
  225.  
  226. throw std::invalid_argument("wrong index");
  227. }
  228.  
  229.  
  230. template<class Type>
  231. Type List<Type>::getItem(int index) const
  232. {
  233. if(getSize()>=0 && index<=size-1)
  234. {
  235. int counter=0;
  236. Node<Type> *p=head;
  237.  
  238. while( !(counter==index) )
  239. {
  240. p=p->next;
  241. counter++;
  242. }
  243.  
  244. Type item(p->data);
  245. return item;
  246. }
  247.  
  248. throw std::invalid_argument("wrong index");
  249.  
  250. }
  251.  
  252.  
  253.  
  254.  
  255. /**************************************************************************/
  256. // List Operations
  257.  
  258. // Adds a node to the start of the list, making it the new head
  259. template <class Type>
  260. void List<Type>::add(Type x)
  261. {
  262. Node<Type> *p = new Node<Type>; //temporary node
  263.  
  264. p -> data = x;
  265. p -> next = head;
  266. head = p;
  267. size++;
  268. }
  269.  
  270. // Inserts a new node at the given position (or index) in the list
  271. template <class Type>
  272. void List<Type>::insertAt(int pos, Type x)
  273. {
  274. Node<Type> *p;
  275. Node<Type> *newNode;
  276.  
  277. // Check that pos is a valid index
  278. if (pos <= size)
  279. {
  280. newNode = new Node<Type>; //new node
  281. newNode->data = x;
  282.  
  283. // Deal with case when item is to be inserted at the head of the list
  284. if (pos == 0)
  285. {
  286. newNode->next = head;
  287. head = newNode;
  288. }// if(pos == 0)
  289. else
  290. {
  291. p = head;
  292. // Move to position BEFORE insertion point
  293. for(Type i = 0; i < pos-1; i++)
  294. {
  295. // Check that p points to a node
  296. // Note using exception handling should throw an exception here
  297. if(p == NULL)
  298. {
  299. return;
  300. }
  301. p = p->next;
  302. }//for
  303. // Insert node
  304. newNode->next = p->next;
  305. p->next = newNode;
  306. }//else (pos != 0)
  307. size++;
  308. }//else (pos >= size) do nothing
  309. }
  310.  
  311. // Removes the first node containing the given data, returns true
  312. // iff data is found (and deleted).
  313. template <class Type>
  314. bool List<Type>::remove(Type x)
  315. {
  316. Node<Type> *p = head;
  317. Node<Type> *temp;
  318. // Check to see if the list exists
  319. if (head == NULL)
  320. {
  321. return false;
  322. }
  323. // Check to see if target is at the head of the list
  324. else if (head->data == x)
  325. {
  326. head = head->next;
  327. delete p; //currently assigned head
  328. size--;
  329. return true;
  330. }
  331. // Otherwise iterate through list
  332. else
  333. {
  334. while(p->next != NULL)
  335. {
  336. // Check next node for target
  337. if(p->next->data == x)
  338. {
  339. temp = p->next;
  340. p->next = p->next->next;
  341. size--;
  342. delete temp;
  343. return true;
  344. }
  345. p = p->next;
  346. }
  347. }
  348. return false;
  349. }
  350.  
  351. template <class Type>
  352. // Empties the list by deleting each node, starting at the head
  353. void List<Type>::removeAll()
  354. {
  355. Node<Type> *p = head;
  356. // Traverse the list deleting nodes
  357. while (p!= NULL)
  358. {
  359. head = head->next;
  360. delete p; // De-allocate memory
  361. p = head; // Go to next node
  362. }
  363. head = NULL;
  364. size = 0;
  365. }
  366.  
  367. template <class Type>
  368. void List<Type>::printList()
  369. {
  370. Node<Type> *p = head;
  371. cout << "["; //Nice format!
  372. // Traverse the list
  373. while (p != NULL)
  374. {
  375. cout << p -> data; // Print data
  376. p = p -> next; // Go to next node
  377. if (p != NULL)
  378. {
  379. cout << ","; // Print a comma unless at the end of the list
  380. }
  381. }
  382. cout << "]"; // Don't print a newline - it might not be wanted
  383. }
  384.  
  385. #endif // LIST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement