Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.85 KB | None | 0 0
  1. /******************************************************************************/
  2. /*!
  3. \file List.cpp
  4. \author Brady Menendez
  5. \par email: brady.m\@digipen.edu
  6. \par DigiPen login: brady.m
  7. \par Course: CS170
  8. \par Section A
  9. \par Lab 6
  10. \date 2/20/2017
  11. \brief
  12. This is the implementation file for all member functions
  13. of a class called List, as specified in specification
  14. file List.h.
  15.  
  16. Operations include:
  17. + List
  18. + ~List
  19. + MakeNode
  20. + Count
  21. + AddToEnd
  22. + AddToFront
  23. + FindItem
  24. + Insert
  25. + Concat
  26. + Delete
  27. + operator<<
  28.  
  29. Hours spent on this assignment : 1 hour
  30.  
  31. Specific portions that gave you the most trouble:
  32. Fixing Dr Memory warnings.
  33.  
  34. */
  35. /******************************************************************************/
  36. #include <iostream> // cout
  37. #include "List.h"
  38.  
  39. namespace CS170
  40. {
  41. namespace ListLab
  42. {
  43. /*!*************************************************************************
  44. \brief
  45. Creates a list and sets the head to 0.
  46. ***************************************************************************/
  47. List::List()
  48. {
  49. head_ = 0;
  50. }
  51.  
  52. /*!*************************************************************************
  53. \brief
  54. Creates a new node with a given value.
  55.  
  56. \param value The value for the new node to hold
  57. \return Created node
  58. ***************************************************************************/
  59. Node* List::MakeNode(int value) const
  60. {
  61. // The Node constructor sets the value, and sets next to 0.
  62. return new Node(value);
  63. }
  64.  
  65. /*!*************************************************************************
  66. \brief
  67. Prints all the nodes in the list.
  68.  
  69. \param os - the ostream
  70. \param list - the list to be printed
  71. ***************************************************************************/
  72. std::ostream& operator<<(std::ostream &os, const List& rhs)
  73. {
  74. Node *list = rhs.head_;
  75. while(list)
  76. {
  77. os << list->number << " ";
  78. list = list->next;
  79. }
  80.  
  81. return os;
  82. }
  83.  
  84. /*!*************************************************************************
  85. \brief
  86. Counts the nodes in the list.
  87.  
  88. \return count The number of nodes in the list.
  89. ***************************************************************************/
  90. int List::Count() const
  91. {
  92. Node *list = head_; // node pointer to the head
  93. int count = 0; // sets counter to 0
  94. while (list) // goes through the list
  95. {
  96. count++; // increments the counter
  97. list = list->next; // goes to the next node
  98. }
  99. return count; // returns the number of nodes
  100. }
  101.  
  102. /*!*************************************************************************
  103. \brief
  104. Adds a node to the end of the list.
  105.  
  106. \param value The value the new node is gonna hold.
  107. ***************************************************************************/
  108. void List::AddToEnd(int value)
  109. {
  110. Node *newNode = MakeNode(value); // node pointer
  111. Node *temp = head_; // temp node pointer
  112.  
  113. if (temp == NULL) // checks if temp is NULL
  114. {
  115. head_ = newNode; // pointes to pNode
  116. return;
  117. }
  118.  
  119. while (temp->next) // checks if not NULL
  120. {
  121. temp = temp->next; // sets temp to next node
  122. }
  123. temp->next = newNode; // sets the node next to temp to a new node
  124. }
  125.  
  126. /*!*************************************************************************
  127. \brief
  128. Adds a node to the front of the list
  129.  
  130. \param value The value the new node is gonna hold.
  131. ***************************************************************************/
  132. void List::AddToFront(int value)
  133. {
  134. Node *newNode = MakeNode(value); // makes new node
  135. newNode->next = head_; // puts new node at the beginning of the list
  136.  
  137. head_ = newNode; // sets new head
  138. }
  139.  
  140. /*!*************************************************************************
  141. \brief
  142. Finds an item.
  143.  
  144. \param value The value the new node is gonna hold.
  145. \return The node with the value to find
  146. ***************************************************************************/
  147. Node *List::FindItem(int value) const
  148. {
  149. Node *pNode = head_; // node pointer to the head
  150.  
  151. while (pNode) // goes through the list
  152. {
  153. if (pNode->number == value) // checks for the value needed
  154. return pNode;
  155.  
  156. pNode = pNode->next; // goes to the next node
  157. }
  158. return NULL;
  159. }
  160.  
  161. /*!*************************************************************************
  162. \brief
  163. Deletes the entire linked list.
  164. ***************************************************************************/
  165. List::~List()
  166. {
  167. while (head_) // goes through the list
  168. {
  169. Node *temp = head_->next; // node pointer to the node after the head
  170. delete head_; // deletes the head
  171. head_ = temp; // sets new head
  172. }
  173. }
  174.  
  175. /*!*************************************************************************
  176. \brief
  177. Inserts a new node at the specified position
  178.  
  179. \param value The value the new node is gonna hold.
  180. \param position the position in the list to put the node in.
  181. ***************************************************************************/
  182. void List::Insert(int value, int position)
  183. {
  184. if (position == 0) // checks for first node in the list
  185. {
  186. AddToFront(value); // adds new node to the front of the list
  187. return;
  188. }
  189.  
  190. if (Count() < position) // checks if position exceeds the number of nodes
  191. return;
  192.  
  193. if (position == Count()) // checks if position is at the end of the list
  194. {
  195. AddToEnd(value); // adds node to the end of the list
  196. return;
  197. }
  198.  
  199. Node *newNode = MakeNode(value); // makes new node
  200. Node *pNode = head_; // node pointer to head
  201.  
  202. for (int i = 0; i < (position - 1); i++) // checks for position
  203. {
  204. pNode = pNode->next; // goes to next node
  205. }
  206.  
  207. newNode->next = pNode->next; // inserts node
  208.  
  209. pNode->next = newNode;
  210. }
  211.  
  212. /*!*************************************************************************
  213. \brief
  214. Inserts a new node at the specified position
  215.  
  216. \param value The value the new node is gonna hold.
  217. ***************************************************************************/
  218. void List::Insert(int value)
  219. {
  220.  
  221. Node *pNode = head_; // node pointer to head
  222.  
  223. if (Count() == 0) // checks if there is no nodes in the list
  224. {
  225. AddToFront(value); // adds it to the front
  226. return;
  227. }
  228.  
  229. for (int i = 0; i < Count(); i++) // goest through the list
  230. {
  231. // checks if the first node is greater than the value
  232. if(pNode->number > value && i == 0)
  233. {
  234. AddToFront(value); // adds a new node to the front
  235. return;
  236. }
  237.  
  238. // checks if the node is lesser than the previous one and greater than
  239. // the next one
  240. if(i <= (Count() - 2) && pNode->number <= value &&
  241. pNode->next->number > value)
  242. {
  243. Node *newNode = MakeNode(value); // makes new node
  244. newNode->next = pNode->next; // links new node and the next node
  245. pNode->next = newNode; // links the previous node to the new one
  246. return;
  247. }
  248.  
  249. // checks if last node is less than the value
  250. if(i == (Count() - 1) && pNode->number < value)
  251. {
  252. AddToEnd(value); // adds new node to the end of the list
  253. return;
  254. }
  255.  
  256. pNode = pNode->next; // goes to next node
  257. }
  258. }
  259.  
  260. /*!*************************************************************************
  261. \brief
  262. Deletes a node with a certain value.
  263.  
  264. \param value The value to destroy.
  265. ***************************************************************************/
  266. void List::Delete(int value)
  267. {
  268. Node *pNode = head_; // pointer to the head
  269.  
  270. if (!FindItem(value)) // checks if the value is in the list
  271. {
  272. return;
  273. }
  274. for (int i = 0; i < Count(); i++) // goes through each node
  275. {
  276. // checks if the first node has the value
  277. if (pNode->number == value && i == 0)
  278. {
  279. head_ = pNode->next; // moves head to next node
  280. delete pNode; // deletes previous head
  281. return;
  282. }
  283.  
  284. // checks if next node holds the value
  285. if (pNode->next->number == value
  286. && i < (Count() - 2))
  287. {
  288. Node *temp = pNode->next->next; // pointer to node after that one
  289. delete pNode->next; // deletes next node
  290. pNode->next = temp; // sets next node to the temp one
  291. return;
  292. }
  293.  
  294. // checks if last node matches the value
  295. if (pNode->next->number == value
  296. && i == Count() - 2)
  297. {
  298. delete pNode->next; // deletes the node
  299. pNode->next = NULL; // sets pointer to the last node to null
  300. return;
  301. }
  302.  
  303. pNode = pNode->next; // goes to the next node
  304. }
  305. }
  306.  
  307. /*!*************************************************************************
  308. \brief
  309. Deletes all the values from a given list based on another one.
  310.  
  311. \param Items The List to substract from the other one.
  312. ***************************************************************************/
  313. void List::Delete(const List& Items)
  314. {
  315.  
  316. Node *list2 = Items.head_; // list 2 head pointer
  317.  
  318. for (int i = 0; i < Items.Count(); i++) // goes through each node
  319. {
  320. Delete(list2->number); // deletes that value from the first list
  321. list2 = list2->next; // goes to the next node
  322. }
  323. }
  324.  
  325. /*!*************************************************************************
  326. \brief
  327. Adds one list to the end of another one.
  328.  
  329. \param Source The List to add to the other one.
  330. ***************************************************************************/
  331. void List::Concat(const List& Source)
  332. {
  333. Node *list2 = Source.head_; // list 2 head pointer
  334.  
  335. for (int i = 0; i < Source.Count(); i++) // goes through each node
  336. {
  337. AddToEnd(list2->number); // adds to the end of the first list2
  338. list2 = list2->next; // goes to the next node
  339. }
  340. }
  341. } // namespace ListLab
  342. } // namespace CS170
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement