Advertisement
Guest User

Untitled

a guest
Apr 24th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. numberlist.cpp
  2.  
  3. // Implementation file for the NumberList class
  4. #include <iostream> // For cout and NULL
  5. #include "NumberList.h"
  6. using namespace std;
  7.  
  8. //**************************************************
  9. // appendNode appends a node containing the *
  10. // value pased into num, to the end of the list. *
  11. //**************************************************
  12.  
  13. void NumberList::appendNode(int num)
  14. {
  15. ListNode *newNode; // To point to a new node
  16. ListNode *nodePtr; // To move through the list
  17.  
  18. // Allocate a new node and store num there.
  19. newNode = new ListNode;
  20. newNode->value = num;
  21. newNode->next = NULL;
  22.  
  23. // If there are no nodes in the list
  24. // make newNode the first node.
  25. if (!head)
  26. head = newNode;
  27. else // Otherwise, insert newNode at end.
  28. {
  29. // Initialize nodePtr to head of list.
  30. nodePtr = head;
  31.  
  32. // Find the last node in the list.
  33. while (nodePtr->next)
  34. nodePtr = nodePtr->next;
  35.  
  36. // Insert newNode as the last node.
  37. nodePtr->next = newNode;
  38. }
  39. }
  40.  
  41. //**************************************************
  42. // displayList shows the value *
  43. // stored in each node of the linked list *
  44. // pointed to by head. *
  45. //**************************************************
  46.  
  47. void NumberList::displayList() const
  48. {
  49. ListNode *nodePtr; // To move through the list
  50.  
  51. // Position nodePtr at the head of the list.
  52. nodePtr = head;
  53.  
  54. // While nodePtr points to a node, traverse
  55. // the list.
  56. while (nodePtr)
  57. {
  58. // Display the value in this node.
  59. cout << nodePtr->value << endl;
  60.  
  61. // Move to the next node.
  62. nodePtr = nodePtr->next;
  63. }
  64. }
  65.  
  66. //**************************************************
  67. // The insertNode function inserts a node with *
  68. // num copied to its value member. *
  69. //**************************************************
  70.  
  71. void NumberList::insertNode(int num)
  72. {
  73. ListNode *newNode; // A new node
  74. ListNode *nodePtr; // To traverse the list
  75. ListNode *previousNode = NULL; // The previous node
  76.  
  77. // Allocate a new node and store num there.
  78. newNode = new ListNode;
  79. newNode->value = num;
  80.  
  81. // If there are no nodes in the list
  82. // make newNode the first node
  83. if (!head)
  84. {
  85. head = newNode;
  86. newNode->next = NULL;
  87. }
  88. else // Otherwise, insert newNode
  89. {
  90. // Position nodePtr at the head of list.
  91. nodePtr = head;
  92.  
  93. // Initialize previousNode to NULL.
  94. previousNode = NULL;
  95.  
  96. // Skip all nodes whose value is less than num.
  97. while (nodePtr != NULL && nodePtr->value < num)
  98. {
  99. previousNode = nodePtr;
  100. nodePtr = nodePtr->next;
  101. }
  102.  
  103. // If the new node is to be the 1st in the list,
  104. // insert it before all other nodes.
  105. if (previousNode == NULL)
  106. {
  107. head = newNode;
  108. newNode->next = nodePtr;
  109. }
  110. else // Otherwise insert after the previous node.
  111. {
  112. previousNode->next = newNode;
  113. newNode->next = nodePtr;
  114. }
  115. }
  116. }
  117.  
  118. //**************************************************
  119. // The deleteNode function searches for a node *
  120. // with num as its value. The node, if found, is *
  121. // deleted from the list and from memory. *
  122. //**************************************************
  123.  
  124. void NumberList::deleteNode(int num)
  125. {
  126. ListNode *nodePtr; // To traverse the list
  127. ListNode *previousNode; // To point to the previous node
  128.  
  129. // If the list is empty, do nothing.
  130. if (!head)
  131. return;
  132.  
  133. // Determine if the first node is the one.
  134. if (head->value == num)
  135. {
  136. nodePtr = head->next;
  137. delete head;
  138. head = nodePtr;
  139. }
  140. else
  141. {
  142. // Initialize nodePtr to head of list
  143. nodePtr = head;
  144.  
  145. // Skip all nodes whose value member is
  146. // not equal to num.
  147. while (nodePtr != NULL && nodePtr->value != num)
  148. {
  149. previousNode = nodePtr;
  150. nodePtr = nodePtr->next;
  151. }
  152.  
  153. // If nodePtr is not at the end of the list,
  154. // link the previous node to the node after
  155. // nodePtr, then delete nodePtr.
  156. if (nodePtr)
  157. {
  158. previousNode->next = nodePtr->next;
  159. delete nodePtr;
  160. }
  161. }
  162. }
  163.  
  164. //**************************************************
  165. // Destructor *
  166. // This function deletes every node in the list. *
  167. //**************************************************
  168.  
  169. NumberList::~NumberList()
  170. {
  171. ListNode *nodePtr; // To traverse the list
  172. ListNode *nextNode; // To point to the next node
  173.  
  174. // Position nodePtr at the head of the list.
  175. nodePtr = head;
  176.  
  177. // While nodePtr is not at the end of the list...
  178. while (nodePtr != NULL)
  179. {
  180. // Save a pointer to the next node.
  181. nextNode = nodePtr->next;
  182.  
  183. // Delete the current node.
  184. delete nodePtr;
  185.  
  186. // Position nodePtr at the next node.
  187. nodePtr = nextNode;
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement