结构体的定义如下:
typedef struct dataRecord {
        //will complie the struct according to the FLAG
       int                     iUnitFlag;
       char              cData[MAX_CDATA];
}DataRecord,* PDataRecord;链表中的结构定义如下:
struct Node //スレオケ
    {
        struct Node *last; ーメサク
        char        *element; 
        struct Node *next; ヨクマサクレオ
    };
    struct Node *m_head; 
我已经把typedef struct dataRecord 中的数据插入到struct Node中的element中,现在我需要从struct Node的element取出,复制到typedef struct dataRecord 中,如何
操作,我的处理如下:bool  CLinkList::Search(const char *element, int &position, DataRecord * dataRecord)
{
DataRecord   *dataRecordTemp;
dataRecordTemp = dataRecord;
memcpy(dataRecordTemp,temp->element,sizeof(element));
...............}
为何会出现数据丢失呢?????
    トク

解决方案 »

  1.   

    //insert the record to LinkNode
    void CFlowChartApp::InsertDataToLinkNode(DataRecord * PDataRecordTemp)
    {
    if (PDataRecordTemp == NULL)
    return;
    DataRecord * PDataRecord = PDataRecordTemp;
    //m_CobArray.SetAt(0, *PDataRecordTemp);

    m_CobTable.Count(iInsertNum);
    if (iInsertNum < 0)
    return;
    if (m_CobTable.Insert(++iInsertNum, (char *)PDataRecord) == true)
    {
    //AfxMessageBox("Insert Succeed !");
    }
    else
    {
    AfxMessageBox("Insert Failed!");
    return ;
    }
    }//&#65428;&#65434;&#65430;&#65400;&#65398;&#65384;&#65422;&#65403;&#65430;&#65411;&#65417;&#65423;&#65420;&#64018;&#65427;&#65405;&#65434;&#65397;&#12539;
    bool CLinkList::Insert(int position, const char *element)
    {
        int index = 0;
        int len   = 0;
        //&#65409;&#65433;&#65418;&#65393;&#65397;&#65412;&#65405;&#65434;&#65397;聊&#65400;&#65429;&#12539;
        struct Node *temp    = NULL;
        struct Node *back    = NULL;
        struct Node *newNode = NULL;    if ((position < 1) || (position > (m_count+1))) //&#65430;&#65400;&#65398;&#65384;&#65397;&#65412;&#65422;&#65403;&#65430;&#65411;&#65394;&#65403;&#65429;&#63729;&#65416;&#65399;
        {
            return false;
        }
        if (m_head != NULL) //&#65393;楨&#65399;&#65430;&#65400;&#65429;&#12539;&#65403;&#65422;&#65386;&#65407;&#65429;
        {
            //temp = m_head->next; //&#65430;&#65400;&#65423;&#65434;&#65426;&#65403;&#65400;&#65434;&#65397;&#12539; //by echo
    temp = m_head;  //the head point        //&#65409;&#65396;&#65393;﨔&#65386;&#65407;&#65429;&#65379;&#65388;&#65411;&#65403;&#65427;&#65424;&#65416;&#65422;&#65402;&#65422;&#65405;&#65434;&#65397;飜&#65393;
            if (NULL != temp && m_count == 0)
            {
                newNode = new Node; //&#65422;&#65386;&#65397;&#65434;&#65426;&#65403;&#65400;&#65434;&#65397;翹&#12539;&#12539;&#65434;&#65396;豼&#65429;&#65404;&#12539;
                if (newNode != NULL) //&#65412;&#65434;&#65396;賈&#12539;&#12539;&#65417;&#65401;&#65382;
                {
                    //m_head->next = newNode; //&#65393;楨&#65399;&#65430;&#65400;&#65429;&#12539;&#65400;&#65423;&#65434;&#65426;&#65403;&#65400;&#65434;&#65397;&#12539; //by echo
    m_head = newNode;   //the head point
    newNode->last = m_head; //&#65397;&#65434;&#65426;&#65403;&#65400;&#65434;&#65397;聊&#65400;&#65423;楨&#65399;&#65430;&#65400;&#65429;&#12539; 
                    //&#65407;&#65405;&#65393;&#65396;&#65405;&#65434;&#65397;耿&#65386;&#65419;&#65432;&#65430;&#65397;
                    len = strlen(element);
                    newNode->element = new char[len+1];
                    strcpy(newNode->element, element);
    newNode->next = NULL; //&#65411;&#65403;&#65427;&#65424;&#65402;&#65420;&#65405;&#65434;&#65397;&#12539;
    ++m_count; //&#65405;&#65434;&#65397;羣&#63729;&#65404;&#65427;1
                    return true;
                }
                else //&#65412;&#65434;&#65396;賈&#12539;&#12539;&#65383;&#65392;&#65436;
                {
                    return false;
                }
            } //end of if        //&#65428;&#65434; 1&#65377;&#65378;1&#65427;&#12539;_count&#65430;&#65390;&#65404;茖&#65378;m_count &#65422;&#65403;&#65430;&#65411;&#65417;&#65423;&#65394;衒&#12539;&#65434;&#65397;飜&#65393;
            while (temp != NULL)  //the head point is existing 
            {
                ++index;
                if (position == index) //&#65429;&#65426;&#65397;&#65405;&#65414;&#65381;&#65413;莊&#65412;&#65422;&#65403;&#65430;&#65411;
                {
                    newNode = new Node; //&#65422;&#65386;&#65394;衒&#12539;&#65412;&#65405;&#65434;&#65397;翹&#12539;&#12539;&#65434;&#65396;豼&#65429;&#65404;&#12539;
                    if (newNode != NULL) //&#65412;&#65434;&#65396;賈&#12539;&#12539;&#65417;&#65401;&#65382;
                    {
                        //temp->last->next = newNode; //&#65418;&#65401;&#65415;&#65392;&#65426;&#65403;&#65400;&#65434;&#65397;聊&#65400;&#65423;&#65410;&#65396;&#65396;&#65405;&#65384;&#65397;&#65412;&#65405;&#65434;&#65397;&#12539;
    temp = newNode;   //let the pri point the new point   
                        newNode->last = temp->last; //&#65418;&#65401;&#65424;&#65410;&#65396;&#65396;&#65405;&#65384;&#65397;&#65412;&#65405;&#65434;&#65397;聊&#65400;&#65423;&#65392;&#65426;&#65403;&#65400;&#65434;&#65397;&#12539;
                        //&#65407;&#65405;&#65393;&#65396;&#65405;&#65434;&#65397;耿&#65386;&#65419;&#65432;&#65430;&#65397;
                        len = strlen(element);
                        newNode->element = new char[len+1];
                        strcpy(newNode->element, element);
                        newNode->next = temp; //&#65418;&#65401;&#65424;&#65410;&#65396;&#65396;&#65405;&#65384;&#65397;&#65412;&#65405;&#65434;&#65397;聊&#65400;&#65423;&#65411;&#65422;&#65403;&#65430;&#65411;&#65417;&#65423;&#65428;&#65389;&#65408;&#65396;&#65397;&#65412;&#65405;&#65434;&#65397;罐&#65388;&#65404;&#65396;&#65423;&#65430;&#65428;&#65434;&#65397;&#65412;&#65423;&#65410;&#65426;&#65403;&#65400;&#65434;&#65397;罍&#65379;
                        temp->last = newNode; //&#65400;&#65411;&#65422;&#65403;&#65430;&#65411;&#65417;&#65423;&#65428;&#65389;&#65408;&#65396;&#65397;&#65412;&#65405;&#65434;&#65397;&#12539;&#65404;&#65396;&#65423;&#65430;&#65428;&#65434;&#65397;&#65412;&#65423;&#65410;&#65426;&#65403;&#65400;&#65434;&#65397;&#12539;&#65430;&#65400;&#65423;&#65410;&#65396;&#65396;&#65405;&#65384;&#65397;&#65412;&#65405;&#65434;&#65397;&#12539;
                        ++m_count; //&#65405;&#65434;&#65397;羣&#63729;&#65404;&#65427;1
                        return true;
                    }
                    else //&#65412;&#65434;&#65396;賈&#12539;&#12539;&#65383;&#65392;&#65436;
                    {
                        return false;
                    }
                } //end of if
                back = temp; //&#65393;&#65379;&#65396;貮&#65392;&#65426;&#65403;&#65400;&#65434;&#65397;羞&#65412;&#65430;&#65400;&#65429;&#12539;
                temp = temp->next; //&#65430;&#65400;&#65423;&#65410;&#65426;&#65403;&#65400;&#65434;&#65397;&#12539;
            } //end of while
            //&#65428;&#65434;&#65422;&#65403;&#65430;&#65411; m_count+1 &#65417;&#65423;&#65394;衒&#12539;&#65434;&#65397;飜&#65393;
            newNode = new Node; //&#65422;&#65386;&#65394;衒&#12539;&#65412;&#65405;&#65434;&#65397;翹&#12539;&#12539;&#65434;&#65396;豼&#65429;&#65404;&#12539;
            if (newNode != NULL) //&#65412;&#65434;&#65396;賈&#12539;&#12539;&#65417;&#65401;&#65382;
            {
                back->next = newNode; //&#65418;&#65401;&#65415;&#65392;&#65426;&#65403;&#65400;&#65434;&#65397;聊&#65400;&#65423;&#65410;&#65396;&#65396;&#65405;&#65384;&#65397;&#65412;&#65405;&#65434;&#65397;&#12539;
                newNode->last = back; //&#65418;&#65401;&#65424;&#65410;&#65396;&#65396;&#65405;&#65384;&#65397;&#65412;&#65405;&#65434;&#65397;聊&#65400;&#65423;&#65392;&#65426;&#65403;&#65400;&#65434;&#65397;&#12539;
                //&#65407;&#65405;&#65393;&#65396;&#65405;&#65434;&#65397;耿&#65386;&#65419;&#65432;&#65430;&#65397;
                len = strlen(element);
                newNode->element = new char[len+1];
                strcpy(newNode->element, element);
                newNode->next = NULL; //&#65411;&#65403;&#65427;&#65424;&#65402;&#65420;&#65405;&#65434;&#65397;&#12539;
                ++m_count; //&#65405;&#65434;&#65397;羣&#63729;&#65404;&#65427;1
                return true;
            }
            else //&#65412;&#65434;&#65396;賈&#12539;&#12539;&#65383;&#65392;&#65436;
            {
                return false;
            }
        }
        else //&#65393;楨&#65399;&#65430;&#65400;&#65429;&#12539;&#65386;&#65407;&#65429;
        {
            return false;
        }
    }
    插入的代码如上,请看看对吗?
      

  2.   


    bool CLinkList::Insert(int position, const char *element)
    {
        int index = 0;
        int len   = 0;    struct Node *temp    = NULL;
        struct Node *back    = NULL;
        struct Node *newNode = NULL;    if ((position < 1) || (position > (m_count+1))) 
        {
            return false;
        }
        if (m_head != NULL) 
        {
            //temp = m_head->next;  //by echo
    temp = m_head;  //the head point        
            if (NULL != temp && m_count == 0)
            {
                newNode = new Node; 
                if (newNode != NULL) 
                {
                    //m_head->next = newNode; 
    m_head = newNode;   
    newNode->last = m_head;  
                    
                    len = strlen(element);
                    newNode->element = new char[len+1];
                    strcpy(newNode->element, element);
    newNode->next = NULL; 
    ++m_count; 
                    return true;
                }
                else 
                {
                    return false;
                }
            } //end of if        
            while (temp != NULL)  //the head point is existing 
            {
                ++index;
                if (position == index) 
                {
                    newNode = new Node; 
                    if (newNode != NULL) 
                    {
                        //temp->last->next = newNode; 
    temp = newNode;   //let the pri point the new point   
                        newNode->last = temp->last; 
                        
                        len = strlen(element);
                        newNode->element = new char[len+1];
                        strcpy(newNode->element, element);
                        newNode->next = temp; 
                        temp->last = newNode; 
                        return true;
                    }
                    else /
                    {
                        return false;
                    }
                } //end of if
                back = temp; 
                temp = temp->next; 
            } //end of while
            
            newNode = new Node; 
            if (newNode != NULL) 
            {
                back->next = newNode; 
                newNode->last = back; 
               
                len = strlen(element);
                newNode->element = new char[len+1];
                strcpy(newNode->element, element);
                newNode->next = NULL; 
                ++m_count; 
                return true;
            }
            else 
            {
                return false;
            }
        }
        else 
        {
            return false;
        }
    }
      

  3.   

    我在插入的时候:
    //insert the record to LinkNode
    void CFlowChartApp::InsertDataToLinkNode(DataRecord * PDataRecordTemp)
    {
    if (PDataRecordTemp == NULL)
    return;
    DataRecord * PDataRecord = PDataRecordTemp;
    //m_CobArray.SetAt(0, *PDataRecordTemp);

    m_CobTable.Count(iInsertNum);
    if (iInsertNum < 0)
    return;
    if (m_CobTable.Insert(++iInsertNum, (char *)PDataRecord) == true)
    {
    //AfxMessageBox("Insert Succeed !");
    }
    else
    {
    AfxMessageBox("Insert Failed!");
    return ;
    }
    }其中DataRecord * PDataRecord = PDataRecordTemp;在指针PDataRecord中的数据都有,但执行if (m_CobTable.Insert(++iInsertNum, (char *)PDataRecord) == true)进行了类型转换,需要调用
    bool CLinkList::Insert(int position, const char *element)这个函数,因为其第二个参数为const char *element,但这应该不会丢失数据啊.
    请帮我看看插入的此函数Insert()是否有错.
    我取数据的时候进行了转换,
    int index = 0;
        struct Node *temp = NULL; 
        position = 0;
        if (m_head != NULL) 
        {
            temp = m_head; 
            if (temp != NULL) 
            {
                while (temp != NULL)
                {
                    ++index;
           DataRecord * dataRecordTemp;
           dataRecordTemp = dataRecord;
           dataRecordTemp = (DataRecord *)temp->element;
                    //但这里dataRecordTemp 数据就乱掉了????
    ................................
    }
      

  4.   

    //serach infor
    bool  CLinkList::Search(const char *element, int &position, DataRecord * dataRecord)
    {
       int index = 0;
        struct Node *temp = NULL; 
        position = 0;
        if (m_head != NULL) 
        {
            temp = m_head; 
            if (temp != NULL) 
            {
                while (temp != NULL)
                {
                    ++index;
           DataRecord * dataRecordTemp;
           dataRecordTemp = dataRecord;
           dataRecordTemp = (DataRecord *)temp->element;
                    //但这里dataRecordTemp 数据就乱掉了????
    ................................
    }
      

  5.   

    在你的代码中,我加了这一句:
              //added by handwolf 2004-12-1 (begin)
                        //把前面的指针和刚插入的衔接起来,下面前面指向刚插入的~
                        if(back != NULL)
                            back->next = newNode;
                        else 
                            m_head = newNode;
                        //added by handwolf 2004-12-1 (end) bool CLinkList::Insert(int position, const char *element)
    {
        int index = 0;
        int len   = 0;    struct Node *temp    = NULL;
        struct Node *back    = NULL;
        struct Node *newNode = NULL;    if ((position < 1) || (position > (m_count+1))) 
        {
            return false;
        }
        if (m_head != NULL) 
        {
            //temp = m_head->next;  //by echo
    temp = m_head;  //the head point        
            if (NULL != temp && m_count == 0)
            {
                newNode = new Node; 
                if (newNode != NULL) 
                {
                    //m_head->next = newNode; 
    m_head = newNode;   
    newNode->last = m_head;  
                    
                    len = strlen(element);
                    newNode->element = new char[len+1];
                    strcpy(newNode->element, element);
    newNode->next = NULL; 
    ++m_count; 
                    return true;
                }
                else 
                {
                    return false;
                }
            } //end of if        
            while (temp != NULL)  //the head point is existing 
            {
                ++index;
                if (position == index) 
                {
                    newNode = new Node; 
                    if (newNode != NULL) 
                    {
                        //temp->last->next = newNode; 
    temp = newNode;   //let the pri point the new point   
                        newNode->last = temp->last; 
                        
                        len = strlen(element);
                        newNode->element = new char[len+1];
                        strcpy(newNode->element, element);
                        newNode->next = temp; 
                        temp->last = newNode;                    //added by handwolf 2004-12-1 (begin)
                        //把前面的指针和刚插入的衔接起来,下面前面指向刚插入的~
                        if(back != NULL)
                            back->next = newNode;
                        else 
                            m_head = newNode;
                        //added by handwolf 2004-12-1 (end)                     return true;
                    }
                    else /
                    {
                        return false;
                    }
                } //end of if
                back = temp; 
                temp = temp->next; 
            } //end of while
            
            newNode = new Node; 
            if (newNode != NULL) 
            {
                back->next = newNode; 
                newNode->last = back; 
               
                len = strlen(element);
                newNode->element = new char[len+1];
                strcpy(newNode->element, element);
                newNode->next = NULL; 
                ++m_count; 
                return true;
            }
            else 
            {
                return false;
            }
        }
        else 
        {
            return false;
        }
    }
      

  6.   

    我帮你写了下面的这个函数,注意:接口也改了!我觉得你以前的第一个参数没用,所以给删除了!
    还有,最好你可以自己调试一下,看看里面的内容!
    bool  CLinkList::Search(int &position, DataRecord * dataRecord)
    {
        DataRecord   *dataRecordTemp;
        dataRecordTemp = dataRecord;
        int index = 0;
        int len   = 0;    struct Node *temp    = NULL;   
        if ((position < 1) || (position > (m_count+1)) || dataRecord == NULL)//增加
        {
            return false;
        }
        if (m_head != NULL) 
        {
            temp = m_head;  //the head point         
            while (temp != NULL)  //the head point is existing 
            {
                ++index;
                if (position == index) 
                {
                  len = strlen(temp->element);
                  //changed by handwolf  
           memcpy(dataRecordTemp->cData,temp->element,len); 
                  return true;
                }   
                temp = temp->next; 
            } //end of while
            
        }
        else 
        {
            return false;
        }
      return false;
    }
      

  7.   

    bool  CLinkList::Search(int &position, DataRecord * dataRecord)
    {
        DataRecord   *dataRecordTemp;
        dataRecordTemp = dataRecord;
        int index = 0;
        int len   = 0;    struct Node *temp    = NULL;   
        if ((position < 1) || (position > (m_count+1)) || dataRecord == NULL)//增加
        {
            return false;
        }
        if (m_head != NULL) 
        {
            temp = m_head;  //the head point         
            while (temp != NULL)  //the head point is existing 
            {
                ++index;
                if (position == index) 
                {
                  len = strlen(temp->element);
                  //changed by handwolf  
           memcpy(dataRecordTemp->cData,temp->element,len); 
                  return true;
                }   
                temp = temp->next; 
            } //end of while
            
        }
        else 
        {
            return false;
        }
      return false;
    }