Question

I made a doubly linked list in my MFC program. Whenever I want to load the variables, the program crashes. I cant create a new Node. Does anyone know how to Serialize a doubly linked list.

Here is my function:

void CDatenbankDoc::Serialize(CArchive& ar)
{
    if (ar.IsStoring())
    {
        Actual = Start;
        while (Actual)
        {   
            ar << Actual->name;
            ar << Actual->adresse;
            ar << Actual->email;
            ar << Actual->fax;
            ar << Actual->firma;
            ar << Actual->geburtsdatum;
            ar << Actual->geschlecht;
            ar << Actual->land;
            ar << Actual->ort;
            ar << Actual->plz;
            ar << Actual->telefon;
            ar << Actual->vorname;
            Actual = Actual->next;      
        }
    }
    else
    {
        Actual = Start;
        while (InsertedAll != true)
        {
            Actual->next = new Node;
            Actual->next->previous = Actual;
            ar >> Actual->next->name;
            ar >> Actual->next->vorname;
            ar >> Actual->next->adresse;
            ar >> Actual->next->email;
            ar >> Actual->next->fax;
            ar >> Actual->next->firma;
            ar >> Actual->next->geburtsdatum;
            ar >> Actual->next->geschlecht;
            ar >> Actual->next->land;
            ar >> Actual->next->ort;
            ar >> Actual->next->plz;
            ar >> Actual->next->telefon;
            Actual = Actual->next;
            if (!Actual->next)
            {
                InsertedAll = true;
            }
        }
    }
}
Was it helpful?

Solution

Try the code below. I've not tested it, so there might be some errors, but you should get the idea. I assume that Start is a member of CDatenbankDoc.

int CDatenbankDoc::GetLinkedListSize()
{
  // This function returns the length of the linked list whose 1st element 
  // is pointed by the Start member.
  // The implementation is left as an exercice

}    

void CDatenbankDoc::Serialize(CArchive& ar)
{
    if (ar.IsStoring())
    {
        ar << GetLinkedListSize(Start) ;  // save size of linked list

        Actual = Start;
        while (Actual)
        {   
            ar << Actual->name;
            ar << Actual->adresse;
            ...
            Actual = Actual->next;      
        }
    }
    else
    {
        int size ;
        ar >> size ;   // get size of linked list

        Node *previous = NULL ;

        for (int i = 0; i < size; i++)
        {
            Node *Actual = new Node;     // assuming Node constructor inits
                                         // pointers to Null                
            if (previous)
                previous->next = actual ;

            Actual->previous = previous ;

            if (i == 0)
              Start = Actual ;

            // beware, the order of serialisation and deserialisazion must 
            // be the same during reading and writing which is not the case
            // in the code you posted !!!

            ar >> Actual->name;
            ar >> Actual->adresse;
            ...

            previous = Actual ;
        }
    }
}

A better solution would be to encapsulate your linked list into a class that has a Serialize member fonction.

OTHER TIPS

Your if (!Actual->next) accesses a value that hasn't been initialized.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top