Reputation: 1189
I'm trying to implement linked list in c++.
I implement my assignment operator like this:
// assignment operator
template<class T>
LinkedList<T>& LinkedList<T>::operator = (const LinkedList& rhs) {
if (&rhs != this) {
Node *tmp = head;
while (tmp -> next) {
head = head -> next;
delete tmp;
tmp = head;
}
tmp = rhs -> head;
while (tmp) {
append(tmp);
tmp = tmp -> next;
}
}
return *this;
}
In my main function, i use the following code to test:
LinkedList<int> *lst1 = new LinkedList<int>(7);
LinkedList<int> *lst2 = new LinkedList<int>(101);
std::cout << lst1 -> head -> data << std::endl;
std::cout << lst2 -> head -> data << std::endl;
lst1 = lst2;
std::cout << lst1 -> head -> data << std::endl;
delete lst1;
delete lst2; <--------------- error here
As I expect, the console outputs:
7 101 101
But when the program try to delete lst2, i get an error saying:
pointer being freed was not allocated
I use debugger and find out when the program is doing assignment:
lst1 = lst2;
lst1 is actually referring to the address that points lst2 instead of getting a copy of lst2, so when i delete lst1, lst2 is already gone.
So can anyone please tell me what is wrong with my assignment operator?
I'm sorry if this is a novice question but I've been spending a few hours and could not figure out.
My completed code is shown below:
template<class T>
class LinkedList {
private:
class Node {
public:
T data;
Node *next;
// default constructor
Node() = default;
// constructor with data
Node(const T& data) : data(data), next(NULL) {}
};
public:
Node *head;
LinkedList(const LinkedList& copyLst);
LinkedList& operator=(const LinkedList& byValList);
LinkedList() : head(NULL){}
LinkedList(Node *newNode) : head(newNode) {}
LinkedList(T val) {
head = new Node(val);
}
~LinkedList();
static LinkedList<int> sumLists(const LinkedList<int>& lst1, const LinkedList<int>& lst2) ;
void insertAtFront(T val);
void insertAtEnd(T val);
void printList();
void insert(T val);
void append(const Node&);
};
// copy constructor
template<class T>
LinkedList<T>::LinkedList(const LinkedList<T>& copyLst) {
const Node *cpCurrent = copyLst.head;
Node *lsCurrent = NULL;
if (cpCurrent != NULL) {
head = new Node(cpCurrent -> data);
lsCurrent = head;
cpCurrent = cpCurrent -> next;
}
while (cpCurrent != NULL) {
Node *newNode = new Node(cpCurrent -> data);
lsCurrent -> next = newNode;
lsCurrent = lsCurrent -> next;
cpCurrent = cpCurrent -> next;
}
}
// assignment operator
template<class T>
LinkedList<T>& LinkedList<T>::operator = (const LinkedList& rhs) {
if (&rhs != this) {
Node *tmp = head;
while (tmp -> next) {
head = head -> next;
delete tmp;
tmp = head;
}
tmp = rhs -> head;
while (tmp) {
append(tmp);
tmp = tmp -> next;
}
}
return *this;
}
// destructor
template<class T>
LinkedList<T>::~LinkedList() {
Node *current = head;
while (current != NULL) {
head = head -> next;
delete current;
current = head;
}
}
template<typename T>
void LinkedList<T>::append(const Node& node ){
if (NULL == head) {
Node *newNode = new Node(node -> data);
head = newNode;
} else {
Node *current = head;
while (current -> next) {
current = current -> next;
}
Node *newNode = new Node(node -> data);
current -> next = newNode;
}
}
Upvotes: 3
Views: 36716
Reputation: 35440
Your current implementation duplicates code that already exists in the copy constructor, so why not reuse it?
If you have a working copy constructor and destructor, usage of the copy / swap idiom would be the easiest and safest way to implement the assignment operator.
#include <algorithm>
//...
template<class T>
LinkedList<T>& LinkedList<T>::operator = (const LinkedList<T>& rhs)
{
LinkedList<T> temp(rhs);
std::swap(temp.head, head);
return *this;
}
Given that your copy constructor and destructor work correctly, this is guaranteed to work correctly. We create a copy temp
of the object rhs
and swap out its contents with the contents of *this
. When temp
gets destroyed at the return, it takes along with it the old data that used to be in *this
.
If you have a C++ 11 compiler, you can take advantage of move construction on the passed-in parameter with pass-by-value.
#include <algorithm>
//...
template<class T>
LinkedList<T>& LinkedList<T>::operator = (LinkedList<T> rhs)
{
std::swap(rhs.head, head);
return *this;
}
* It should be noted that you need to swap ALL members of the class when using the copy and swap idiom, otherwise you will likely invalidate the class members' invariants *
Upvotes: 7