结构体的定义如下:
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));
...............}
为何会出现数据丢失呢?????
トク
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));
...............}
为何会出现数据丢失呢?????
トク
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 ;
}
}//ヤレヨクカィホサヨテノマフ晴モスレオ・
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; //ア楨キヨクユ・クマレメサクレオ・ //by echo
m_head = newNode; //the head point
newNode->last = m_head; //オレメサクレオ聊クマ楨キヨクユ・
//ソスアエスレオ耿ェヒリヨオ
len = strlen(element);
newNode->element = new char[len+1];
strcpy(newNode->element, element);
newNode->next = NULL; //テサモミコフスレオ・
++m_count; //スレオ羣シモ1
return true;
}
else //トレエ賈・・ァーワ
{
return false;
}
} //end of if //ヤレ 1。「1モ・_countヨョシ茖「m_count ホサヨテノマイ衒・レオ飜ア
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; //クテホサヨテノマヤュタエオトスレオ・シエマヨヤレオトマツメサクレオ・ヨクマツエエスィオトスレオ・
++m_count; //スレオ羣シモ1
return true;
}
else //トレエ賈・・ァーワ
{
return false;
}
} //end of if
back = temp; //ア」エ貮ーメサクレオ羞トヨクユ・
temp = temp->next; //ヨクマツメサクレオ・
} //end of while
//ヤレホサヨテ m_count+1 ノマイ衒・レオ飜ア
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; //スレオ羣シモ1
return true;
}
else //トレエ賈・・ァーワ
{
return false;
}
}
else //ア楨キヨクユ・ェソユ
{
return false;
}
}
插入的代码如上,请看看对吗?
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;
}
}
//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 数据就乱掉了????
................................
}
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 数据就乱掉了????
................................
}
//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;
}
}
还有,最好你可以自己调试一下,看看里面的内容!
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;
}
{
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;
}