fork download
  1. #ifndef LIST_H
  2. #define LIST_H
  3.  
  4. #include <string>
  5. #include <fstream>
  6.  
  7. typedef std::string String;
  8.  
  9. template <class T>
  10. struct Node {
  11. T data;
  12. Node *mNext;
  13. };
  14.  
  15. template <typename T>
  16. class List
  17. {
  18.  
  19. public:
  20. //Default constructor
  21. List();
  22.  
  23. //Copy constructor
  24. List(const List& copy);
  25.  
  26. //Overloaded assignment operator
  27. List &operator=(const List& rhs);
  28.  
  29. //Destructor
  30. ~List();
  31.  
  32. //Methods
  33. void PushFront(const T& T);
  34. void PushBack(const T& T);
  35. void PopFront() throw(String);
  36. void PopBack();
  37. T &Front() const;
  38. T &Back() const;
  39. void Purge();
  40. bool empty();
  41. int getNumberOfNodes();
  42. Node<T>* CreateNode(const T&);
  43. void Save(std::ofstream & file);
  44. void Load(std::ifstream & file);
  45. T &GetIthElement(int index) throw(int);
  46. void Display();
  47.  
  48. private:
  49. //Data members
  50. Node<T>* mHead;
  51. Node<T>* mTail;
  52. int mNumberOfNodes;
  53. static int mNodes;
  54. };
  55.  
  56. #endif
  57.  
  58.  
  59. template <typename T>
  60. List<T>::List()
  61. :mHead(nullptr), mNumberOfNodes(0)
  62. {
  63. }
  64.  
  65.  
  66. template <typename T>
  67. List<T>::List(const List<T> & copy)
  68. :mHead(nullptr), mNumberOfNodes(0)
  69. {
  70. *this = copy;
  71. }
  72.  
  73.  
  74. template <typename T>
  75. List<T> &List<T>::operator=(const List<T> & rhs)
  76. {
  77. //Check for self-assignment
  78. if(this != &rhs)
  79. {
  80. //Deallocate previously allocated memory
  81. Purge();
  82.  
  83. Node<T> * travel = rhs.mHead;
  84.  
  85. //Deep copy
  86. while(travel != nullptr)
  87. {
  88. this->PushFront(this->CreateNode(travel->getData()));
  89. travel = travel->mNext;
  90. }
  91. }
  92. //Return invoking object
  93. return *this;
  94. }
  95.  
  96.  
  97. template <typename T>
  98. List<T>::~List()
  99. {
  100. Node<T> * trail = nullptr;
  101. while(mHead != nullptr)
  102. {
  103. trail = mHead->mNext;
  104. delete mHead;
  105. mHead = trail;
  106. }
  107. }
  108.  
  109.  
  110. template <typename T>
  111. void List<T>::PushFront(const T& T)
  112. {
  113. newNode->mNext = mHead;
  114. mHead = newNode;
  115.  
  116. //Increase number of nodes
  117. mNumberOfNodes++;
  118. }
  119.  
  120.  
  121. template <typename T>
  122. void List<T>::PushBack(const T& T)
  123. {
  124. Node<T> * travel = nullptr;
  125.  
  126. //Check to see if the list is empty
  127. if(mHead == '\0')
  128. mHead = newNode;
  129. else
  130. {
  131. travel = mHead;
  132.  
  133. //Get to the end of the list
  134. while(travel->mNext != '\0')
  135. travel = travel->mNext;
  136.  
  137. //Append new node
  138. travel->mNext = newNode;
  139.  
  140. //Increase number of nodes
  141. mNumberOfNodes++;
  142. }
  143. }
  144.  
  145. template <typename T>
  146. void List<T>::PopFront()
  147. {
  148. Node<T> * headPointer = nullptr;
  149.  
  150. if(mHead == nullptr)
  151. throw "\nEmpty List!\n";
  152. else
  153. {
  154. headPointer = mHead->mNext;
  155. delete mHead;
  156. mHead = headPointer;
  157. mNumberOfNodes--;
  158. }
  159. }
  160.  
  161. template <typename T>
  162. void List<T>::PopBack()
  163. {
  164. Node<T> * travel = nullptr;
  165. Node<T> * trail = nullptr;
  166.  
  167. //Check to see if the list is empty
  168. if(mHead == '\0')
  169. std::cout << "\nCannot delete an empty list! " << std::endl;
  170. else
  171. {
  172. travel = mHead;
  173.  
  174. //Get to the end of the list
  175. while(travel->mNext != '\0')
  176. {
  177. trail = travel;
  178. travel = travel->mNext;
  179. }
  180.  
  181. //Delete last node
  182. delete travel;
  183.  
  184. //Make new last node equal to null
  185. trail->mNext = nullptr;
  186. }
  187.  
  188. //Decrease number of Nodes
  189. mNumberOfNodes--;
  190. }
  191.  
  192.  
  193. //const Potion &List::Front() const
  194. template <typename T>
  195. T &List<T>::Front() const
  196. {
  197. Node<T> * headPointer = nullptr;
  198.  
  199. if ( this->mHead != nullptr )
  200. headPointer = this->mHead;
  201. else
  202. throw "\nEmpty list! ";
  203.  
  204. return headPointer->getData();
  205. }
  206.  
  207. template <typename T>
  208. T &List<T>::Back() const
  209. {
  210. Node<T> * travel = this->mHead;
  211.  
  212. if ( travel != nullptr )
  213. {
  214. while ( travel->mNext != nullptr )
  215. travel = travel->mNext;
  216. }
  217. else
  218. throw "\nEmpty list! ";
  219.  
  220. return travel->getData();
  221. }
  222.  
  223. template <typename T>
  224. void List<T>::Purge()
  225. {
  226. Node<T> * travel = mHead;
  227. Node<T> * trail = nullptr;
  228.  
  229. while(travel != nullptr)
  230. {
  231. trail = travel;
  232. travel = travel->mNext;
  233. delete trail;
  234. }
  235. mHead = nullptr;
  236.  
  237. //Decrease number of nodes
  238. mNumberOfNodes--;
  239. }
  240.  
  241. template <typename T>
  242. int List<T>::getNumberOfNodes()
  243. {
  244. return mNumberOfNodes;
  245. }
  246.  
  247. template <typename T>
  248. Node<T> * List<T>::CreateNode(const T & T)
  249. {
  250. //Allocate space for new node
  251. Node<T> * newNode = new Node;
  252.  
  253. //Assign data to new node
  254. newNode->mData = T;
  255.  
  256. //newNode->mData.setName(potion.getName());
  257. //newNode->mData.setDescription(potion.getDescription());
  258. //newNode->mData.setPotency(potion.getPotency());
  259. //newNode->mData.setCost(potion.getCost());
  260.  
  261. //Assign new node's next
  262. newNode->mNext = nullptr;
  263.  
  264. //Increase number of nodes
  265. mNumberOfNodes++;
  266.  
  267. return newNode;
  268. }
  269.  
  270. template <typename T>
  271. bool List<T>::empty()
  272. {
  273. if(mNumberOfNodes == 0)
  274. return true;
  275. else
  276. return false;
  277. }
  278.  
  279. template <typename T>
  280. void List<T>::Save(std::ofstream & file)
  281. {
  282. //Write the number of nodes
  283. file.write(reinterpret_cast<char *>(&mNumberOfNodes), sizeof(int));
  284.  
  285. Node<T> * travel = this->mHead;
  286.  
  287. while( travel != nullptr )
  288. {
  289. travel->getData().Save(file);
  290. travel = travel->mNext;
  291. }
  292. }
  293.  
  294. template <typename T>
  295. void List<T>::Load(std::ifstream & file)
  296. {
  297. //Read the number of elements
  298. file.read(reinterpret_cast<char *>(&mNumberOfNodes), sizeof(int));
  299.  
  300. //Insert nodes into list
  301. T temp;
  302.  
  303. for( int i = 0; i < mNumberOfNodes; i++ )
  304. {
  305. temp.Load(file);
  306. this->PushFront(CreateNode(temp));
  307. mNumberOfNodes--;
  308. mNumberOfNodes--;
  309. }
  310. }
  311.  
  312. template <typename T>
  313. T &List<T>::GetIthElement(int index)
  314. {
  315. Node<T> * trail = this->mHead;
  316.  
  317. if(index > mNodes)
  318. throw 666;
  319.  
  320. else
  321. {
  322. for(int i = 0; i < index; i++)
  323. trail = trail->mNext;
  324. }
  325.  
  326. return trail->getData();
  327. }
  328.  
  329. template <typename T>
  330. void List<T>::Display()
  331. {
  332. Node<T>* travel = mHead;
  333. int i = 0;
  334.  
  335. while(travel != nullptr)
  336. {
  337. this->GetIthElement(i).Display();
  338. NewLine();
  339. travel = travel->mNext;
  340. i++;
  341. }
  342. }
  343.  
  344. template <typename T>
  345. int List<T>::mNodes = 2;
  346.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout

Standard output is empty