Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. Note: See "linktester.cpp" for more information.
  2. */
  3.  
  4. #ifndef LINKEDLIST_CPP
  5. #define LINKEDLIST_CPP
  6.  
  7. #include <iostream>
  8. #include <new>
  9. using namespace std;
  10.  
  11. #include "LinkedList.h"
  12.  
  13. LinkedList::LinkedList()
  14. {
  15. first = 0;
  16. mySize = 0;
  17. }
  18.  
  19. LinkedList::LinkedList(const LinkedList & origList)
  20. {
  21. mySize = origList.mySize;
  22.  
  23. if(origList.mySize == 0)
  24. {
  25. first = NULL;
  26. }
  27. else
  28. {
  29. Node * origPtr = origList.first;
  30. LinkedList::NodePointer lastPtr = new(nothrow) Node(origPtr->data);
  31. first = lastPtr;
  32. origPtr = origPtr->next;
  33.  
  34. for(int i = 0; i < size()-1; i++)
  35. {
  36. lastPtr->next = new Node(origPtr->data);
  37. lastPtr = lastPtr->next;
  38. origPtr = origPtr->next;
  39. }
  40.  
  41.  
  42. }
  43. }
  44.  
  45.  
  46. LinkedList::~LinkedList()
  47. {
  48. Node * ptr = first;
  49. for(int i = 0; i < size(); i++)
  50. {
  51. first = ptr -> next;
  52. delete ptr;
  53. ptr = first;
  54. }
  55.  
  56. if(first == 0) cout << "List destroyed\n";
  57. else cout << "List not destroyed\n";
  58. }
  59.  
  60. int LinkedList::size()
  61. {
  62. return mySize;
  63. }
  64.  
  65. void LinkedList::display(ostream & out) const
  66. {
  67. LinkedList::NodePointer ptr = first;
  68. while(ptr != NULL)
  69. {
  70. out << ptr->data << " ";
  71. ptr = ptr->next;
  72. }
  73.  
  74. }
  75.  
  76. void LinkedList::insert(int index, int dataValue)
  77. {
  78.  
  79. LinkedList::NodePointer nPtr = new(nothrow) Node(dataValue);
  80. if(index > size()|| index < 0)
  81. {
  82. cout << "Index out of range. " << endl;
  83. return;
  84. }
  85.  
  86. mySize = mySize+1;
  87.  
  88. if(index == 0) // insert at the beginning of the list
  89. {
  90. nPtr->next = first;
  91. first = nPtr;
  92. }
  93. else
  94. {
  95. LinkedList::NodePointer predPtr = first;
  96. for(int i = 0; i < (index-1); i++)
  97. {
  98. predPtr = predPtr->next;
  99. }
  100.  
  101. nPtr->next = (predPtr->next);
  102. predPtr->next = (nPtr);
  103. }
  104. }
  105.  
  106. void LinkedList::erase(int index)
  107. {
  108. LinkedList::NodePointer prevPtr = first;
  109. LinkedList::NodePointer ptr = first;
  110.  
  111. if(index > (LinkedList::size()-1) || index < 0) //out of range
  112. {
  113. cout << "The requested index to erase is out of range. " << endl;
  114. return;
  115. }
  116.  
  117. if(index == 0) // erase first item
  118. {
  119. first = prevPtr->next;
  120. }
  121.  
  122. else
  123. {
  124. Node * predPtr = first;
  125. for(int i = 0; i < index-1; i++)
  126. {
  127. predPtr= predPtr->next; //get to the proper index in the list
  128. }
  129.  
  130. ptr = predPtr->next; // ptr points to the node to be deleted.
  131.  
  132. if(ptr->next != NULL)
  133. {
  134. predPtr->next = ptr->next;
  135. delete ptr;
  136. }
  137. else
  138. {
  139. delete ptr;
  140. predPtr->next = NULL;
  141. }
  142.  
  143. }
  144.  
  145. mySize = mySize-1;
  146.  
  147. return;
  148.  
  149.  
  150. }
  151.  
  152. ostream & operator<< (ostream & out, const LinkedList &list)
  153. {
  154. list.display(out);
  155. return out;
  156. }
  157.  
  158. LinkedList& LinkedList::operator=(const LinkedList& list)
  159. {
  160. if(this == &list)
  161. return *this;
  162. else
  163. {
  164. this->~LinkedList();
  165.  
  166. LinkedList l(list);
  167. return *this;
  168.  
  169. }
  170. }
  171.  
  172. LinkedList::Node::Node(int dataVal)
  173. {
  174. data = dataVal;
  175. next = NULL;
  176. }
  177.  
  178.  
  179.  
  180.  
  181. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement