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:

// ----------------------------------------------------------------------------------------
// 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;
}

Again disregard what i asked for above, i've worked out something that works for me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.