I am still trying to learn more about copy and move constructors. I have a linked list class that I want to deep copy using copy and move constructors but I'm having issues. First, to deep copy List class, do I only copy head_ and tail_ in the constructors. I know the code is horrendous, maybe I shouldn't jump into high-level stuff right away.
Any help is appreciated!
template<typename T>
class List
{
public:
class Node {
public:
Node(T value) : value_(value) {}
T value_;
Node* next_;
Node* prev_;
};
Node* head_;
Node* tail_;
//! Default constructor
List() :tail_(nullptr) {}
//! Copy constructor
List(const List& lst) : head_(nullptr) {
//not sure what goes in here
}
}
//! Move constructor
List(List&& move) {
head_ = move.head_;
move.head_ = nullptr;
tail_ = move.tail_;
move.tail_ = nullptr;
}
//! Copy assignment operator
List& operator= (const List& list) {
tail_ = nullptr;
head_ = tail_;
Node* current = list.head_;
Node* next = list.head_->next_;
Node* replace = head_;
while (next != list.tail_) {
current = current->next_;
next = next->next_;
replace->next_ = tail_;
replace->next_->value_;
replace = replace->next_;
}
return *this;
}
//! Move assignment operator
List& operator= (List&& other) {
tail_ = nullptr;
head_ = tail_;
head_->next_ = other.head_->next_;
Node* current = other.head_;
Node* next = other.head_->next_;
while (next != other.tail_) {
current = current->next_;
next = next->next_;
}
current->next_ = tail_;
other.head_->next_ = other.tail_;
return *this;
}
Here is my five cents.:)
The demonstrative program below shows how the copy constructor, move constructor, copy assignment operator, move assignment operator, and the destructor can be implemented including some other auxiliary functions.
#include <iostream>
#include <utility>
#include <functional>
#include <iterator>
template<typename T>
class List
{
private:
struct Node
{
T value;
Node *prev;
Node *next;
} *head = nullptr, *tail = nullptr;
void copy( const List &list )
{
if ( list.head )
{
head = tail = new Node { list.head->value, nullptr, nullptr };
for ( Node *current = list.head->next; current; current = current->next )
{
tail = tail->next = new Node { current->value, tail, nullptr };
}
}
}
public:
//! Default constructor
List() = default;
//! Copy constructor
List( const List &list )
{
copy( list );
}
// Constructor with iterators
template <typename InputIterator>
List( InputIterator first, InputIterator last )
{
if ( first != last )
{
head = tail = new Node { *first, nullptr, nullptr };
while ( ++first != last )
{
tail = tail->next = new Node { *first, tail, nullptr };
}
}
}
// Destructor
~List()
{
clear();
}
//! Move constructor
List( List &&list )
{
std::swap( head, list.head );
std::swap( tail, list.tail );
}
//! Copy assignment operator
List & operator =( const List &list )
{
clear();
copy( list );
return *this;
}
//! Move assignment operator
List & operator =( List &&list )
{
std::swap( head, list.head );
std::swap( tail, list.tail );
return *this;
}
void clear()
{
while ( head )
{
delete std::exchange( head, head->next );
}
tail = head;
}
void push_front( const T &value )
{
head = new Node{ value, nullptr, head };
if ( !tail )
{
tail = head;
}
else
{
head->next->prev = head;
}
}
void push_back( const T &value )
{
Node *new_node = new Node{ value, tail, nullptr };
if ( tail )
{
tail = tail->next = new_node;
}
else
{
head = tail = new_node;
}
}
friend std::ostream & operator <<( std::ostream &os, const List &list )
{
for ( Node *current = list.head; current; current = current->next )
{
os << current->value << " -> ";
}
return os << "null";
}
};
int main()
{
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List<int> list1( std::begin( a ), std::end( a ) );
std::cout << list1 << '\n';
list1 = List<int>( std::rbegin( a ), std::rend( a ) );
std::cout << list1 << '\n';
}
The program output is
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 -> null
For example in this statement
list1 = List<int>( std::rbegin( a ), std::rend( a ) );
there is used the move assignment operator.
Because List tracks the tail, you can call a function that adds items to the end of the list without the added overhead of iterating to the end of the list.
List(const List& lst) : head_(nullptr), tail_(nullptr)
{
Node * cur = lst.head_; //get first source item.
while (cur) // if there is a source item to copy
{
push_back(cur->value_); // stick the item on the end of this list
cur = cur->next_; // get next source item
}
}
where push_back looks something like
void push_back(T value)
{
Node * newnode = new Node(value, tail_, nullptr); //make and link new tail node
if (tail_)
{
tail_->next_ = newnode; // link in new node
}
else
{
head_ = newnode;
}
tail_ = newnode; // update tail
}
and Node picked up a new constructor to simplify insertion:
Node(T value,
Node * prev,
Node * next) : value_(value), prev_(prev), next_(next)
{
}
Note:
In the assignment operators,
tail_ = nullptr;
head_ = tail_;
cut the pointers to any data that was in the linked list, leaking those Nodes. You need to free these nodes before replacing them. The Copy and Swap Idiom (What is the copy-and-swap idiom?) makes this easy by using the copy construction and destruction of a local variable to automate the process.
Related
My group and I attempted to implement a queue in C++ based on the following example for the code of a stack.
template<typename T>
class Stack {
private:
struct Node {
T data;
Node* next;
Node(T data, Node* next): data(data), next(next) {}
// No destructor here; it is a private inner class and would be slow
// to delete a chain of nodes this way!
};
private:
Node* head;
public:
Stack(): head(nullptr) {}
~Stack() {
while (head != nullptr) {
Node* old_head = head;
head = head->next; // update
delete old_head;
}
}
// Prohibit copy construction and copy assignment
Stack(Stack&) = delete;
Stack& operator=(Stack&) = delete;
// Wait wait I want to be able to move
Stack(Stack&&) = default;
Stack& operator=(Stack&&) = default;
public:
int getSize() {
int count = 0;
for (Node* n = head; n != nullptr; n = n->next) count++;
return count;
}
bool isEmpty() {
return head == nullptr;
}
void push(T x) {
head = new Node(x, head);
}
T pop() {
if (isEmpty()) {
throw underflow_error("Cannot pop from empty stack");
}
Node* nodeToDelete = head;
T poppedValue = head->data;
head = head->next;
delete nodeToDelete;
return poppedValue;
}
T peek() {
if (isEmpty()) {
throw underflow_error("Cannot peek into empty stack");
}
return head->data;
}
};
Just like the stack example, we attempted to implement move for the queue using the same ideas, including the default move assignment operator. Doing this, we came across problems such as invalid operands errors, collapsed stack frames and more when running our code.
template < typename T >
class Queue {
private:
struct Node {
T data;
Node * next;
Node(T data, Node * next): data(data), next(next) {}
};
private: Node * head;
private: Node * tail;
public:
Queue(): head(nullptr), tail(nullptr) {}~Queue() {
while (head != nullptr) {
Node * old = head;
head = head -> next;
delete old;
}
}
Queue(Queue & ) = delete;
Queue & operator = (Queue & ) = delete;
Queue(Queue && ) = default;
Queue & operator = (Queue && ) = default;
public:
int get_size() {
int count = 0;
for (Node * n = head; n != nullptr; n = n -> next)
count++;
return count;
}
bool isEmpty() {
return head == nullptr;
}
void enqueue(T x) {
Node * temp = new Node {x, nullptr};
if (head == nullptr) {
head = temp;
tail = temp;
} else {
tail -> next = temp;
tail = temp;
}
}
T dequeue() {
if (isEmpty()) {
throw underflow_error("Cannot dequeue from empty queue");
}
Node * nodeToDelete = head;
T poppedValue = head -> data;
head = head -> next;
delete nodeToDelete;
return poppedValue;
}
};
We ended up making our own move assignment operator which the queue worked as expected. The following code is what we ended up with.
Template <typename T>
class Queue {
private:
struct Node {
T data;
Node *next;
Node(T data, Node *next) : data(data), next(next) {}
};
private:
Node *head;
private:
Node *tail;
public:
Queue() : head(nullptr), tail(nullptr) {}
~Queue() {
while (head != nullptr) {
Node *old = head;
head = head->next;
delete old;
}
}
Queue(Queue &) = delete;
Queue &operator=(Queue &) = delete;
Queue(Queue &&) = default;
Queue &operator=(Queue &&other) {
if (&other == this) {
return *this;
}
delete head;
head = other.head;
tail = other.tail;
other.head = nullptr;
other.tail = nullptr;
return *this;
}
public:
int get_size() {
int count = 0;
for (Node *n = head; n != nullptr; n = n->next) {
count++;
}
return count;
}
bool is_empty() {
return head == nullptr;
}
void enqueue(T x) {
Node *temp = new Node{x, nullptr};
if (head == nullptr) {
head = temp;
tail = temp;
} else {
tail->next = temp;
tail = temp;
}
}
T dequeue() {
if (is_empty()) {
throw underflow_error("Cannot dequeue from empty queue");
}
Node *nodeToDelete = head;
T poppedValue = head->data;
head = head->next;
delete nodeToDelete;
return poppedValue;
}
Although we ultimately went with the latter implementation, we were really curious as to why the default move assignment operator worked for the stack and not the queue. We were wondering if it was because we created our own destructor for the class and disabled the copy operator, but we are unsure. Perhaps we were going about the implementation wrong? Any help/ feedback would be much appreciated!
Generally, you should always consider implement your own move assignment operator and move constructor when it comes to dynamic memory allocation.
Your stack one just appeared to work, but actually it is not correct.
Try to add these to your stack, and you will see the problem:
Stack <int> func() {
Stack <int> s;
s.push(3);
return s;
}
int main()
{
Stack <int> b;
b = func(); // assignment move
return 0;
}
func() is returning a Stack with non-null head member variable (this is critical)
When leaving func(), local variable s is destructed
When leaving main(), local variable b is destructed (problem here)
That's why this code causes a double-free problem in your Stack.
(BTW, your Queue assignment move operator may cause memory leak, since delete head; may not be what you want to do.)
I need to make a program which connects two linked lists before I used global pointer for the head of the list, but now I need to make it locally so I can insert new element(node) to each of them, but I have a problem with double-pointer, not sure when to use **, when * and when &. I can find any example similar to that.
Down below is what I have now.
#include<stdio.h>
#include<stdlib.h>
typedef struct element_{
int x;
struct element_ *next;
}element;
void insert(element **head, int x) {
element *new_ = new element;
element *p;
new_->x = x;
new_->next = NULL;
if (head == NULL) {
*head = new_;
return;
}
else {
for (p = *head;p->next != NULL;p = p->next) {}
p->next = new_;
}
}
int main(){
element **head = NULL;
insert(head,1);
insert(head,3);
insert(head,3);
insert(head,4);
for (element *p = *head;p != NULL;p = p->next){
printf("%d ", p->x);
}
}
There is nothing from C++ in the program except the operator new. So if to substitute the operator new for a call of malloc then you will get a pure C program.
So a C looking function insert can be defined like
void insert(element **head, int x)
{
element *new_ = new element;
new_->x = x;
new_->next = NULL;
while ( *head != NULL )
{
head = &( *head )->next;
}
*head = new_;
}
And in main you should write
element *head = NULL;
insert( &head, 1 );
insert( &head, 3 );
insert( &head, 3 );
insert( &head, 4 );
for (element *p = head; p != NULL; p = p->next )
{
printf("%d ", p->x);
}
Something that looks like a C++ function insert can be defined the following way
void insert( element * &head, int x )
{
element *new_ = new element { x, nullptr };
element **current = &head;
while ( *current != NULL )
{
current = &( *current )->next;
}
*current = new_;
}
And in main you should write
element *head = nullptr;
insert( head, 1 );
insert( head, 3 );
insert( head, 3 );
insert( head, 4 );
for (element *p = head; p != nullptr; p = p->next )
{
std::cout << p->x << ' ';
}
But to call the program indeed as C++ program then you should define the list as a class. Moreover if new nodes are appended to the tail of the singly-linked list then you should define the list a singly-linked two-sided list.
Here is a demonstrative program.
#include <iostream>
#include <functional>
class List
{
private:
struct Node
{
int data;
Node *next;
} *head = nullptr, *tail = nullptr;
public:
List() = default;
List( const List & ) = delete;
List & operator =( const List & ) = delete;
~List()
{
clear();
}
void clear()
{
while ( head )
{
delete std::exchange( head, head->next );
}
tail = head;
}
void push_front( int data )
{
head = new Node { data, head };
if ( !tail ) tail = head;
}
void push_back( int data )
{
Node *node = new Node { data, nullptr };
if ( tail )
{
tail = tail->next = node;
}
else
{
head = tail = node;
}
}
friend std::ostream & operator <<( std::ostream &os, const List &list )
{
for ( Node *current = list.head; current; current = current->next )
{
std::cout << current->data << " -> ";
}
return std::cout << "null";
}
};
int main()
{
List list;
list.push_back( 1 );
list.push_back( 3 );
list.push_back( 3 );
list.push_back( 4 );
std::cout << list << '\n';
}
Its output is
1 -> 3 -> 3 -> 4 -> null
Your code is nearly correct C code.
If head in main is a pointer to a pointer to element you have to dynamically allocate memory for it. It makes the code unnecessary complex. I made head in main a pointer to element. But you want to change it's value in insert so you have to pass by reference. The C way of pass by value is to pass the address. Also there is no new in C. Use malloc. And remember to clean up at the end. You have to call one free for each malloc.
If it really is supposed to be C++ code you have much to do. E.g, you wouldn't use pointers to pointers but references, you would use smart pointers instead of dynamic memory allocation, ...
Even though this is not the C++ way of programming it's also valid C++ code (I'm not sure about the headers).
#include <stdio.h>
#include <stdlib.h>
typedef struct element_{
int x;
struct element_ *next;
} element;
void insert(element **head, int x) {
element *new_ = malloc(sizeof(element));
element *p;
new_->x = x;
new_->next = NULL;
if (*head == NULL) {
*head = new_;
return;
} else {
for (p = *head;p->next != NULL;p = p->next) {}
p->next = new_;
}
}
void clean(element **p) {
if ((*p)->next != NULL) clean(&(*p)->next);
free(*p);
*p = NULL;
}
int main(){
element *head = NULL;
insert(&head, 1);
insert(&head, 3);
insert(&head, 3);
insert(&head, 4);
for (element *p = head; p != NULL; p = p->next){
printf("%d ", p->x);
}
clean(&head);
}
I'm creating a linked list from another linked list. But second linked list is not getting formed and there's a memory leak message on running the program.
Here's a section of the code that's troubling-
Node *rearrangeLinkedList(Node *head){
printLinkedList(head);
int lengthoflist = 0;
Node *temp = head;
while (temp!=NULL)
{
lengthoflist++;
temp=temp->next;
}
Node *secondList = NULL;
// just a variable node to store the head of second linked list-
Node *headOfSecondList = secondList;
int count=1;
while (count<=lengthoflist/2)
{
Node *temproary = new Node();
temproary->data=head->data;
temproary->next=NULL;
secondList=temproary;
secondList=secondList->next;
head=head->next;
count++;
}
printLinkedList(headOfSecondList);
}
printLinkedList() function is perfectly printing out the incoming list but not the second linked list.
After
Node *secondList = NULL;
Node *headOfSecondList = secondList;
you don't modify headOfSecondList any more. It will still be NULL when you call
printLinkedList(headOfSecondList); // => printLinkedList(NULL);
But you have another error in the copy-function:
while (count<=lengthoflist / 2)
{
Node *temproary = new Node();
temproary->data=head->data;
temproary->next=NULL;
secondList=temproary; // assign secondList
secondList=secondList->next; // secondList->next is temporary->next is NULL!!
head=head->next;
count++;
}
Here you create a bunch of nodes that all have a next of NULL. You do indeed leak memory here. secondList gets set to NULL at the end of each iteration and when temporary goes out of scope you don't have any pointers to the allocated memory left.
The following should work
// Build first node
Node *secondList = new Node();
secondList->data = head->data;
// advance by one
head = head->next;
// Now this points to the real head instead of NULL
Node *headOfSecondList = secondList;
int count=1;
while (count<=lengthoflist / 2 - 1 ) // -1 since we already handled the head above
{
Node *temproary = new Node(); // new node
temproary->data = head->data; // set data
temproary->next = NULL; // we have no next yet
secondList->next = temproary; // append temporary to secondList
secondList = secondList->next; //advance secondList
head = head->next; // advance head
count++;
}
printLinkedList(headOfSecondList);
I have skipped some validation here, but I hope the basic concept is clearer now.
If I have understood correctly the function tries to build a new list from the first half of nodes of an existed list.
If so then there is no need to calculate the number of nodes in the source list. This is inefficient.
You declared the function having the return type Node *.
Node *rearrangeLinkedList(Node *head );
but the function returns nothing.
Within the function the variable headOfSecondList is set once to nullptr and is never changed.
Node *secondList = NULL;
Node *headOfSecondList = secondList;
Within the while loop new nodes are not chained in a list. There is always changed the variable secondList and its data member next is always set to NULL. So there are numerous memory leaks.
while (count<=lengthoflist/2)
{
Node *temproary = new Node();
temproary->data=head->data;
temproary->next=NULL;
secondList=temproary;
secondList=secondList->next;
head=head->next;
count++;
}
The function can be written the following way.
Node * rearrangeLinkedList( Node *head )
{
Node *new_head = nullptr;
Node **tail = &new_head;
Node *first = head, *current = head;
while ( current != nullptr && ( current = current->next ) != nullptr )
{
current = current->next;
*tail = new Node();
( *tail )->data = first->data;
( *tail )->next = nullptr;
first = first->next;
tail = &( *tail )->next;
}
return new_head;
}
To demonstrate the approach without counting the number of nodes in the source list that as I already pointed out is inefficient here is a demonstrative program with a class template List.
#include <iostream>
template <typename T>
class List
{
private:
struct Node
{
T data;
Node *next;
} *head = nullptr;
public:
List() = default;
~List()
{
while ( head )
{
Node *current = head;
head = head->next;
delete current;
}
}
List( const List<T> & ) = delete;
List<T> & operator =( const List<T> & ) = delete;
void push_front( const T &data )
{
head = new Node { data, head };
}
List<T> & extract_half( List<T> &list ) const
{
Node **tail = &list.head;
while ( *tail ) tail = &( *tail )->next;
Node *first = this->head, *current = this->head;
while ( current != nullptr && ( current = current->next ) != nullptr )
{
current = current->next;
*tail = new Node { first->data, nullptr };
first = first->next;
tail = &( *tail )->next;
}
return list;
}
friend std::ostream & operator <<( std::ostream &os, const List &list )
{
for ( Node *current = list.head; current; current = current->next )
{
os << current->data << " -> ";
}
return os << "null";
}
};
int main()
{
List<int> list1;
const int N = 10;
for ( int i = N; i != 0; )
{
list1.push_front( --i );
}
std::cout << list1 << '\n';
List<int> list2;
list1.extract_half( list2 );
std::cout << list1 << '\n';
std::cout << list2 << '\n';
return 0;
}
The program output is
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
0 -> 1 -> 2 -> 3 -> 4 -> null
I'm trying to write Lined list in C++, but some tests fails.
One of those says:
GivenNonEmptyCollection_WhenMoveAssigning_ThenAllElementsAreMoved
And second:
GivenNonEmptyCollection_WhenMovingToOther_ThenAllItemsAreMoved
Here's how I implement operator=
LinkedList& operator=(const LinkedList& other)
{
if(this!=&other)
{
while (!isEmpty())
erase(begin());
for (auto it = other.begin(); it != other.end(); it++)
append(*it);
}
return *this;}
And second one:
LinkedList& operator=(LinkedList&& other)
{
/* SELF ASSIGNMENT CHECK */
if(this!=&other)
{
while (!isEmpty())
erase(begin());
while (!other.isEmpty())
{
append(*(other.begin()));
other.erase(other.begin());
}
}
return *this;
}
Here's something about class Linked list and struct Node:
template <typename Type>
class LinkedList
{
struct Node
{
Node* prev;
Node* next;
Type* data;
Node()
{
data = nullptr;
prev = nullptr;
next = nullptr;
}
Node(const Type val)
{
data = new Type(val);
prev = nullptr;
next = nullptr;
}
~Node()
{
prev = nullptr;
next = nullptr;
delete data;
}
};
private:
Node *head;
Node *tail;
size_type length;
public:
LinkedList(): head(nullptr), tail(nullptr), length(0)
{
head = new Node;
tail = new Node;
head->next = tail;
tail->prev = head;
}
(...)
I have no idea what's wrong with that.
You're copying and deleting the original list, but you should move it.
In this case, this means "stealing" the data from the other list.
It should look more like this:
LinkedList& operator=(LinkedList&& other)
{
if(this!=&other)
{
// Assuming the existence of 'LinkedList::clear', which empties the list.
// Replace with the name you chose for that function.
clear();
head = other.head;
other.head = nullptr;
tail = other.tail;
other.tail = nullptr;
length = other.length;
other.length = 0;
}
return *this;
}
and your move constructor should be changed similarly.
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;
}
}
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 *