Advertisement
pichumy

Untitled

Feb 8th, 2014
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <iostream>
  4. using namespace std;
  5. #include "IntLinkedList.h"
  6.  
  7. IntLinkedList::IntLinkedList()
  8. {
  9. head = NULL;
  10. tail = NULL;
  11. // add the initialization for the tail pointer
  12. }
  13.  
  14. /* addHead
  15. * add an item to the front of the linked list
  16. *
  17. * TODO: Add code that modifies the tail properly
  18. */
  19.  
  20. void IntLinkedList::addHead(int v)
  21. {
  22. // create a new IntNode and put v into that IntNode
  23. IntNode *l = new IntNode(v);
  24. // if the list is empty, this link is the only thing in it
  25. // all we need to do is point head to the link
  26. if (head == NULL)
  27. {
  28. head = l;
  29. tail = l;
  30. }
  31. // if there is already something there, we point this new link
  32. // to the old head of the list. Then, when we point head to this
  33. // IntNode, it becomes the beginning of the list, and the old beginning
  34. // is now 2nd.
  35. else
  36. {
  37. l->setNext(head);
  38. head = l;
  39. }
  40. }
  41.  
  42. /* addTail
  43. *
  44. * Add an item to the end (tail) of the list
  45. * TODO: Make sure tail is updated properly AND make the code more
  46. * efficient by taking advantage of the tail pointer
  47. */
  48.  
  49. void IntLinkedList::addTail(int v)
  50. {
  51. // make a new link for the new item
  52. IntNode *l = new IntNode(v);
  53. // if the list is empty, this is the only one
  54. if (head == NULL)
  55. {
  56. head = l;
  57. }
  58. // otherwise, we need to go to the back and add the node
  59. else
  60. {
  61. // TODO: Modify this so it takes advantage of tail pointer
  62. // advance l2 so it points to the last link
  63. IntNode *l2;
  64. for(l2 = head;l2->getNext() != NULL; l2 = l2->getNext())
  65. ;
  66. // now add this l to the end by hooking l2 to it
  67. l2->setNext(l);
  68. tail = l2;
  69. }
  70. }
  71.  
  72. /* removeHead
  73. *
  74. * Removes the item at the head of the list
  75. * TODO: Update the tail pointer
  76. */
  77.  
  78. bool IntLinkedList::removeHead()
  79. {
  80. // if the list is empty, return 0
  81. if (head == NULL)
  82. return false;
  83. // set a temporary pointer to head
  84. IntNode *tmp = head;
  85. // advance head so it points to the next one
  86. head = head->getNext();
  87. // delete the IntNode
  88. if(tmp->getNext() == NULL)
  89. tail = NULL;
  90. delete tmp;
  91. // return the value
  92. return true;
  93. }
  94.  
  95. /* removeTail
  96. * removes the last (tail) item from a linked list
  97. * Returns true if it removed something, false if the list was empty
  98. * TODO: Write the code for this method.
  99. * hint: Be very careful about where you stop your loop
  100. */
  101. bool IntLinkedList::removeTail()
  102. {
  103. IntNode *tmp = head;
  104. if(head == NULL)
  105. return false;
  106. if(tail ==NULL)
  107. return false;
  108. if(tmp->getNext() ==NULL)
  109. {
  110. tail = NULL;
  111. head = NULL;
  112. delete tmp;
  113. return true;
  114. }
  115. else
  116. {
  117. while(tmp->getNext()!=tail)
  118. {
  119. tmp = tmp->getNext();
  120. }
  121. delete tail;
  122. tail = tmp;
  123. return true;
  124. }
  125. }
  126.  
  127. /* peekHead
  128. * return the item at the head of the list
  129. * If the list is empty, return 0
  130. */
  131. int IntLinkedList::peekHead()
  132. {
  133. // TODO: fill in this code
  134. if(head == NULL)
  135. return 0;
  136. else
  137. {
  138. IntNode *tmp = head;
  139. return tmp->getValue();
  140. }
  141. }
  142.  
  143. /* peekTail
  144. * return the item at the end (tail) of the list
  145. * If the list is empty, return 0
  146. * TODO: Implement this function. Make sure you take advantage of the tail ptr
  147. */
  148. int IntLinkedList::peekTail()
  149. {
  150. // TODO: fill in this code
  151. if(head == NULL)
  152. return 0;
  153. else
  154. {
  155. IntNode *tmp = tail;
  156. return tmp->getValue();
  157. }
  158. }
  159.  
  160. /* printList
  161. *
  162. * print all of the items in the linked list
  163. */
  164. void IntLinkedList::printList(FILE *f)
  165. {
  166. IntNode *l;
  167. fprintf(f,"List: ");
  168. for(l = head; l != NULL; l = l->getNext())
  169. {
  170. l->printIntNode(f);
  171. fprintf(f, ", ");
  172. }
  173. fprintf(f,"\n");
  174. }
  175.  
  176. IntLinkedList::~IntLinkedList()
  177. {
  178. IntNode *temp = this->head;
  179. while(temp!=NULL)
  180. {
  181. IntNode *temp1 = temp;
  182. temp = temp->getNext();
  183. delete temp1;
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement