Guest User

Untitled

a guest
Apr 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 KB | None | 0 0
  1. #include "LinkedList.h"
  2.  
  3. //
  4. // Default constructor, copy constructor, assignment oerator,
  5. // and destructor
  6.  
  7. static Node *duplicateLinkedList(Node *head);
  8. static Node *searchLinkedList(Datatype value, Node *head);
  9. LinkedList::LinkedList()
  10. {
  11.     head = NULL;
  12. }
  13. LinkedList::LinkedList(const LinkedList& original)
  14. {
  15.     head = duplicateLinkedList(original.head);
  16. }
  17. LinkedList& LinkedList::operator=(const LinkedList& original)
  18. {
  19.     if (&original != this)
  20.     {
  21.         Node *newhead = duplicateLinkedList(original.head);
  22.         removeAll();
  23.         head = newhead;
  24.     }  
  25.     return *this;
  26. }
  27. LinkedList::~LinkedList()
  28. {
  29.     removeAll();
  30.     cout <<"Destructor Call" << endl;
  31. }  
  32. //
  33. // isEmpty
  34. //
  35. // Purpose: Check if a Multiset instance is empty.
  36. // Argument(s): N/A
  37. // Return: True if the Multiset instance is empty, and false
  38. //         otherwise.
  39. //
  40. bool LinkedList::isEmpty() const
  41. {
  42.     return head==NULL;
  43. }
  44. //
  45. // getCount
  46. //
  47. // Purpose: Get the number of items in a Multiset instance.
  48. // Argument(s): N/A
  49. // Return: The number of items currently in the Multiset instance.
  50. //
  51. unsigned int LinkedList::getCount() const
  52. {
  53.     unsigned int count = 0;
  54.     for (Node *p = head; p != NULL; p = p->next)
  55.     {
  56.         count++;
  57.     }
  58.     return count;
  59. }
  60. const Datatype& LinkedList::retrieve(unsigned int index) const
  61. {
  62.     Node *p = head; // make sure to check for output, if wrong head-> next
  63.     for (unsigned int i = 0; i < index; i++)
  64.     {
  65.         p = p -> next;
  66.     }
  67.     return p->datum;
  68.  
  69. }
  70. unsigned int LinkedList::find(const Datatype& value) const
  71. {
  72.     int i = 0;
  73.     for (Node *p = head; p != NULL; p = p->next)
  74.     {
  75.         if (p-> datum == value)
  76.         {
  77.             return i;
  78.         }
  79.         else
  80.         {
  81.             i++;
  82.         }
  83.     }
  84.  
  85.     return NOT_IN_LIST;
  86. }
  87. bool LinkedList::contains(const Datatype& value) const
  88. {
  89.     for (Node *p = head; p != NULL; p = p->next)
  90.     {
  91.         if (p -> datum == value)
  92.         {
  93.             return true;
  94.         }
  95.  
  96.     }
  97.  
  98.     return false;
  99. }
  100. //
  101. // insert
  102. //
  103. // Purpose: Insert an item into a Multiset instance.
  104. // Argument(s):
  105. //   x: A type ItemType item to be inserted into the Multiset instance.
  106. // Side Effect: an additional occurrence of x is in the Multiset instance.
  107. // Return: N/A
  108. //
  109. void LinkedList::insert(const Datatype& value)
  110. {
  111.     // Find the node before the spot to insert
  112.     Node *prev = searchLinkedList(value, head);
  113.     // Create new node
  114.     Node *newp = new Node(value);
  115.     // Insert
  116.     if (prev == NULL)
  117.     {
  118.         newp->next = head;
  119.         head = newp;
  120.     }
  121.     else
  122.     {
  123.         newp->next = prev->next;
  124.         prev->next = newp;
  125.     }
  126. }
  127. //
  128. // removeByIndex
  129. //
  130. // Purpose: Remove an item from a Multiset instance.
  131. // Argument(s):
  132. // index: A index value of the item to be removed from the Multiset instance.
  133. // Return: N/A
  134. //
  135. void LinkedList::removeByIndex(unsigned int index)
  136. {
  137.  
  138.     Node *prev = NULL;
  139.     Node *current = head;
  140.     unsigned int i = 0;
  141.     while (current != NULL && i < index)
  142.     {
  143.         prev = current;
  144.         current = current->next;
  145.         i++;  
  146.     }
  147.     // 2 cases: Target has a predecessor or not
  148.     if (prev != NULL)
  149.     {
  150.         // Target has a predecessor
  151.  
  152.         // Remove
  153.         prev->next = current -> next;
  154.         delete current;
  155.  
  156.     }
  157.     else
  158.     {
  159.  
  160.         head = current->next;
  161.         delete current;
  162.  
  163.     }
  164. }
  165. void LinkedList::removeAll()
  166. {
  167.     Node *current = NULL;
  168.     Node *next = head;
  169.     while (next != NULL)
  170.     {
  171.         current = next;
  172.         next = current->next;
  173.         delete current;
  174.     }
  175.     head = NULL;
  176.  
  177. }
  178. static Node *searchLinkedList(Datatype value, Node *head)
  179. {
  180.     Node *prev = NULL;
  181.     Node *current = head;
  182.     while (current != NULL && current->next != NULL)
  183.     {
  184.         prev = current;
  185.         current = current->next;
  186.     }
  187.     return prev;
  188. }
  189. static Node *duplicateLinkedList(Node *head)
  190. {
  191.     if (head == NULL) return NULL;
  192.     Node *newp = new Node(head->datum);
  193.     Node *prev = newp;
  194.     Node *p = head->next;
  195.     while (p != NULL)
  196.     {
  197.         prev->next = new Node(p->datum);
  198.         prev = prev->next;
  199.         p = p->next;
  200.     }
  201.     prev->next = NULL;
  202.     return newp;
  203. }
Add Comment
Please, Sign In to add comment