I'm just wondering how you would write an assignment operator and copy constructor methods. This has been a big sticking point for me. I know that when you do something like
1 2 3 4
node * head = NULL;
// some form of making nodes that connect, blah blah blah.
node * temp = head;
You can't just assume that when you copy that you're getting everything. And by this, I mean you need to overload the assignment operator so that it will do a deep copy. I'm having trouble writing it. I think it would be easy to do it recursively. Pass in the head node, and check for NULL. If its not NULL, call the method again, until you hit NULL. Also making sure you copy when its not NULL. Any help?
#include "ordered.h"
LinkedList::LinkedList( const LinkedList &other ) // does not work
{
if(other.head == NULL)
return;
}
LinkedList& LinkedList::operator=(const LinkedList & rhs) // does not work
{
if(rhs.head == NULL)
return *this;
return *this;
}
// missing the destructor
void
LinkedList::Append( double value )
{
if( head == NULL )
{
head = new node( value );
return;
}
node * current = head;
while( current->next != NULL)
{
current = current->next;
}
current->next = new node(value);
}
void
LinkedList::Insert( double value )
{
node * temp = new node( value );
// condition for empty list
if( head == NULL )
{
node * temp = new node( value );
temp->next = head;
head = temp;
return;
}
// condition to put the new item before the first item
if(temp->data < head->data)
{
temp->next = head;
head = temp;
return;
}
//initial traversal to correct position in list
node * current = head;
while( (current->data < temp->data) && (current->next != NULL) )
{
current = current->next;
}
// changes to put new item in the list
temp->next = current->next;
current->next = temp;
return;
}
void
LinkedList::Print()
{
node * current = head;
while( current != NULL )
{
cout << current->data << endl;
current = current->next;
}
cout << endl;
}
bool
LinkedList::Find( double value )
{
node * current = head;
int count = 1;
while( current != NULL )
{
if( value == current->data )
{
cout << "Found at position " << count << endl;
returntrue;
}
current = current->next;
count++;
}
cout << "No such value, '" << value << "'" << endl;
returnfalse;
}
bool
LinkedList::RemoveByVal( double value )
{
node * temp = NULL;
// used if first element has value to be removed
if(head->data == value)
{
temp = head->next;
head = temp;
cout << value << " was removed. " << endl;
Print();
returntrue;
}
//initial traversal to correct position in list
node * current = head;
while( (current->data != value) && (current->next != NULL) )
{
if(current->next->data != value)
{
current = current->next;
}
else
{
temp = current->next->next;
current->next = temp;
//delete temp;
cout << value << " was removed. " << endl;
Print();
returntrue;
}
}
cout << value << " wasn't in the list. " << endl;
returnfalse;
}
bool
LinkedList::RemoveByPos( int pos )
{
//initial traversal to correct position in list
node *current = head;
int ct = 1;
while( (ct < pos - 1) && (current->next != NULL))
{
current = current->next;
}
returnfalse;
}
(remember to #include <utility> or #include <algorithm> to get std::swap)
How did you write such a big program without completing the node class, though? Consider testing the components you write before combining them into a larger program.
I was told you shouldn't create a destructor in the node class because its not allocating memory. I was told that I should create a destructor for my linked list class instead.