我在爲我的樹連接節點時遇到問題。在我的程序中,函數read()
將先執行,然後執行load()
函數。在read()
函數中,我的外部文件中的所有數據將被存儲在數組中,然後在load()
函數中重新生成。全局聲明變量dat
和i
。我的問題在於load()
函數。你的幫助是非常讚賞的。順便說一下,我的外部文件數據是按順序排列的,我使用「#」作爲NULL節點。文件重建樹
void read()
{
string dat1;
fstream file;
file.open("data.txt", ios::in);
do
{
getline(file,dat1);
dat[i]=dat1;
i++;
}while(!file.eof());
file.close();
}
void load(node *root,int index)
{
node *nNode;
nNode=(node*)malloc(sizeof(node));
nNode->yes=NULL;
nNode->no=NULL;
nNode->data=dat[index];
if(index<i)
{
if(nNode->data!="#")
{
root=nNode;
load(root->yes,index+1);
load(root->no,index+1);
}
else
{
root=NULL;
return;
}
}
}
你是什麼意思; 「我的問題是在加載函數」是否被竊聽?如果是這樣的話;它編譯?如果不是有什麼錯誤。 – 111111 2012-03-05 16:09:57
它編譯但節點沒有連接。抱歉我的問題。 – user1242812 2012-03-05 16:12:04