•
•
•
•
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
![]() |
•
•
Join Date: Aug 2006
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
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:
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:
c++ Syntax (Toggle Plain Text)
// ---------------------------------------------------------------------------------------- // LinkedList.cpp // // Author: Warren Knox // Version: 01 // Last Revised: 30/10/2006 // Description: A linked list for storing the document ids for all the documents scanned // into the database. // //----------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------- #include "LinkedList.h" #include <stdlib.h> //----------------------------------------------------------------------------------------- // Clears the entire list //----------------------------------------------------------------------------------------- void LinkedList::Clear() { //Start at the beginning of the list Node *start = &m_first; //Loop through and delete until list is empty while (start->GetNext() != NULL) Delete(start); } //----------------------------------------------------------------------------------------- // Deletes the node AFTER the one passed in as a parameter //----------------------------------------------------------------------------------------- void LinkedList::Delete(Node *before) { // Point a temporary point at the one to be deleted Node *temp = before->GetNext(); // Take that one out of the loop before->SetNext(temp->GetNext()); // Delete the unwanted node (release its memory) delete temp; } //----------------------------------------------------------------------------------------- // Deletes the node AFTER the one passed in as a parameter //----------------------------------------------------------------------------------------- bool LinkedList::Remove(int datum) { Node *last, *current; last = &m_first; current = m_first.GetNext(); bool found = false; while (!found && (current != NULL)) { if (datum == current->GetData()) { found = true; last->SetNext(current->GetNext()); delete current; } else { found = false; last = current; current = current->GetNext(); } } return found; } //----------------------------------------------------------------------------------------- // Inserts a new node into the list in ascending order //----------------------------------------------------------------------------------------- bool LinkedList::Insert(int datum) { //Pointer to a new node Node *newNode, *current, *last; bool proceed = true; current = m_first.GetNext(); int count = 0; //Try to create a new node on the heap try { newNode = new Node; } catch(...) { return false; } //Place the data in the new node current = m_first.GetNext(); last = &m_first; proceed = true; newNode->SetData(datum); while (proceed && (current != NULL)) { if (datum >= current->GetData()) { if (datum != current->GetData()) { last = current; current = current->GetNext(); } else { delete newNode; return false; } } else { proceed = false; } } last->SetNext(newNode); newNode->SetNext(current); return true; } //----------------------------------------------------------------------------------------- // Removes the numbers in the calling list that aren't in the new list //----------------------------------------------------------------------------------------- void LinkedList::Joint(const LinkedList &newList) { Node *current, *last, *list2ptr; bool isIn = false; current = m_first.GetNext(); last = &m_first; list2ptr = newList.m_first.GetNext(); while (list2ptr != NULL) { while (!isIn && (current != NULL)) { if (current->GetData() == list2ptr->GetData()) { isIn = true; last->SetNext(current->GetNext()); delete current; } else { last = current; current = current->GetNext(); isIn = false; } } list2ptr = list2ptr->GetNext(); current = m_first.GetNext(); last = &m_first; isIn = false; } } //----------------------------------------------------------------------------------------- ostream &operator << (ostream &ostr, LinkedList &list) { //ostr << "The document numbers contained in the list:"; for (Node *ptr = &(list.m_first); ptr->GetNext() != NULL; ptr = ptr->GetNext()) { ostr << " 0" << ptr->GetNext()->GetData(); } return ostr; } //----------------------------------------------------------------------------------------- Node *LinkedList::Find(int datum) { for (Node *ptr = &m_first; ptr->GetNext() != NULL; ptr = ptr->GetNext()) { if (ptr->GetNext()->GetData() == datum) return ptr; else return &m_first; } } //----------------------------------------------------------------------------------------- LinkedList &LinkedList::operator = (const LinkedList &list) { *this = &list; return *this; }
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- copy constructor & assignment operator overload problems (C++)
- Overloading assignment operator (C++)
- assignment operator for a 3d array (C++)
- Constructor and Convertion operator are the same,how to avoid memory leak? (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ enumerated data type help
- Next Thread: trying to point to an array


Linear Mode