I am having a lot of trouble trying to get my copy constructor, destructor, and assignment operator working for a double linked list. I have a class called dlist and a node class. The Node class contains a private node of next and previous and a data field. I am really stuck and I just can not for the life of me understand how to get these to work. If someone even just points out what's wrong. Sometimes I get a seg fault and other times I get a backtrace, depending on what I change in the big three.
//Destructor
template<class T>
dlist<T>::~dlist(){
node<T> *rmptr = head;
while(head != NULL && head != tail){
head = head -> next();
delete rmptr;
}
}
//Copy Constructor
template <class T>
dlist<T>::dlist(const dlist<T>& other)
{
if(other.head == NULL){
head = new node<T>;
tail -> set_next(head);
}
else{
head = new node<T>(other.head -> data());
tail = new node<T>;
head -> set_next(tail);
tail -> set_previous(head);
node<T> *source = other.head -> next();
node<T> *destination = head;
while(source != NULL && source != other.tail){
tail -> set_next(new node<T>);
destination -> set_next(tail);
tail -> set_data(source -> data());
tail = tail -> next();
source = source -> next();
}
}
}
//Assignment Operator
template<class T>
dlist<T>& dlist<T>::operator =(const dlist& other){
if(this == &other){
return *this;
}
node<T> * rmptr;
while(head != NULL){
rmptr = head;
head = head -> next();
delete rmptr;
}
head = tail = NULL;
node<T> *source, *destination;
if(other.head != NULL){
head = new node<T>(other.head -> data());
tail = new node<T>;
head -> set_next(tail);
tail -> set_previous(head);
node<T> *source = other.head -> next();
node<T> *destination = head;
while(source != NULL && source != other.tail){
tail -> set_next(new node<T>);
destination -> set_next(tail);
tail -> set_data(source -> data());
tail = tail -> next();
source = source -> next();
}
}
return *this;
}