User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,680 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,317 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 735 | Replies: 1 | Solved
Reply
Join Date: Aug 2006
Posts: 8
Reputation: noxee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
noxee noxee is offline Offline
Newbie Poster

Help Assignment operator help

  #1  
Nov 2nd, 2006
Hi I'm trying to have an assignment operator for my linked list class so that when I call it, it replaces the current one with the one I pass in.

i.e testList1 = testList2;

I'm trying to get it so that testList1 will point to testList2.

Here is the code for my linked list class:

  1. // ----------------------------------------------------------------------------------------
  2. // LinkedList.cpp
  3. //
  4. // Author: Warren Knox
  5. // Version: 01
  6. // Last Revised: 30/10/2006
  7. // Description: A linked list for storing the document ids for all the documents scanned
  8. // into the database.
  9. //
  10. //-----------------------------------------------------------------------------------------
  11. //-----------------------------------------------------------------------------------------
  12.  
  13. #include "LinkedList.h"
  14. #include <stdlib.h>
  15.  
  16. //-----------------------------------------------------------------------------------------
  17. // Clears the entire list
  18. //-----------------------------------------------------------------------------------------
  19.  
  20. void LinkedList::Clear()
  21. {
  22. //Start at the beginning of the list
  23. Node *start = &m_first;
  24.  
  25. //Loop through and delete until list is empty
  26. while (start->GetNext() != NULL)
  27. Delete(start);
  28.  
  29. }
  30.  
  31. //-----------------------------------------------------------------------------------------
  32. // Deletes the node AFTER the one passed in as a parameter
  33. //-----------------------------------------------------------------------------------------
  34. void LinkedList::Delete(Node *before)
  35. {
  36. // Point a temporary point at the one to be deleted
  37. Node *temp = before->GetNext();
  38.  
  39. // Take that one out of the loop
  40. before->SetNext(temp->GetNext());
  41.  
  42. // Delete the unwanted node (release its memory)
  43. delete temp;
  44. }
  45.  
  46. //-----------------------------------------------------------------------------------------
  47. // Deletes the node AFTER the one passed in as a parameter
  48. //-----------------------------------------------------------------------------------------
  49. bool LinkedList::Remove(int datum)
  50. {
  51. Node *last, *current;
  52. last = &m_first;
  53. current = m_first.GetNext();
  54. bool found = false;
  55.  
  56. while (!found && (current != NULL))
  57. {
  58. if (datum == current->GetData())
  59. {
  60. found = true;
  61. last->SetNext(current->GetNext());
  62. delete current;
  63. }
  64. else
  65. {
  66. found = false;
  67. last = current;
  68. current = current->GetNext();
  69. }
  70. }
  71.  
  72. return found;
  73. }
  74.  
  75. //-----------------------------------------------------------------------------------------
  76. // Inserts a new node into the list in ascending order
  77. //-----------------------------------------------------------------------------------------
  78. bool LinkedList::Insert(int datum)
  79. {
  80. //Pointer to a new node
  81. Node *newNode, *current, *last;
  82. bool proceed = true;
  83. current = m_first.GetNext();
  84. int count = 0;
  85.  
  86. //Try to create a new node on the heap
  87. try
  88. {
  89. newNode = new Node;
  90. }
  91. catch(...)
  92. {
  93. return false;
  94. }
  95.  
  96.  
  97. //Place the data in the new node
  98. current = m_first.GetNext();
  99. last = &m_first;
  100. proceed = true;
  101. newNode->SetData(datum);
  102.  
  103. while (proceed && (current != NULL))
  104. {
  105. if (datum >= current->GetData())
  106. {
  107. if (datum != current->GetData())
  108. {
  109. last = current;
  110. current = current->GetNext();
  111. }
  112. else
  113. {
  114. delete newNode;
  115. return false;
  116. }
  117. }
  118. else
  119. {
  120. proceed = false;
  121. }
  122. }
  123.  
  124. last->SetNext(newNode);
  125. newNode->SetNext(current);
  126.  
  127. return true;
  128. }
  129.  
  130. //-----------------------------------------------------------------------------------------
  131. // Removes the numbers in the calling list that aren't in the new list
  132. //-----------------------------------------------------------------------------------------
  133. void LinkedList::Joint(const LinkedList &newList)
  134. {
  135. Node *current, *last, *list2ptr;
  136. bool isIn = false;
  137.  
  138. current = m_first.GetNext();
  139. last = &m_first;
  140. list2ptr = newList.m_first.GetNext();
  141.  
  142. while (list2ptr != NULL)
  143. {
  144. while (!isIn && (current != NULL))
  145. {
  146. if (current->GetData() == list2ptr->GetData())
  147. {
  148. isIn = true;
  149. last->SetNext(current->GetNext());
  150. delete current;
  151. }
  152. else
  153. {
  154. last = current;
  155. current = current->GetNext();
  156. isIn = false;
  157. }
  158. }
  159. list2ptr = list2ptr->GetNext();
  160. current = m_first.GetNext();
  161. last = &m_first;
  162. isIn = false;
  163.  
  164. }
  165. }
  166.  
  167. //-----------------------------------------------------------------------------------------
  168. ostream &operator << (ostream &ostr, LinkedList &list)
  169. {
  170. //ostr << "The document numbers contained in the list:";
  171.  
  172. for (Node *ptr = &(list.m_first); ptr->GetNext() != NULL; ptr = ptr->GetNext())
  173. {
  174. ostr << " 0" << ptr->GetNext()->GetData();
  175. }
  176.  
  177. return ostr;
  178. }
  179.  
  180. //-----------------------------------------------------------------------------------------
  181.  
  182. Node *LinkedList::Find(int datum)
  183. {
  184. for (Node *ptr = &m_first; ptr->GetNext() != NULL; ptr = ptr->GetNext())
  185. {
  186. if (ptr->GetNext()->GetData() == datum)
  187. return ptr;
  188. else
  189. return &m_first;
  190. }
  191. }
  192.  
  193. //-----------------------------------------------------------------------------------------
  194.  
  195. LinkedList &LinkedList::operator = (const LinkedList &list)
  196. {
  197. *this = &list;
  198.  
  199. return *this;
  200. }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Posts: 8
Reputation: noxee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
noxee noxee is offline Offline
Newbie Poster

Re: Assignment operator help

  #2  
Nov 2nd, 2006
Again disregard what i asked for above, i've worked out something that works for me
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC