MFC序列化双链接列表

MFC Serialize doubly linked list

本文关键字:列表 链接 序列化 MFC      更新时间:2023-10-16

我在MFC程序中创建了一个双链接列表。每当我想加载变量时,程序就会崩溃。我无法创建新节点。有人知道如何序列化双链表吗。

这是我的功能:

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

尝试下面的代码。我还没有测试过,所以可能会有一些错误,但你应该明白。我假设Start是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 ;
        }
    }
}

更好的解决方案是将链表封装到一个具有Serialize成员函数的类中。

您的if (!Actual->next)访问一个尚未初始化的值。