我有問題返回指向結構的指針。有人可以解釋我做錯了什麼嗎?我想讓search()
返回一個指向匹配輸入的指針。這將被存儲到一個向量中,以防它們在「數組」中重複。這似乎工作,但我無法從指針返回「數據」?結構內的指針問題
struct Node
{
int data;
Node *left;
Node *next;
};
vector<Node *> array;
void find(int & input)
{
currentSize = 0;
vector<Node *> hold;
for(int i = 0; i < array.size(); i++){
if(search(array[i], input) != NULL)
{
hold.push_back(search(array[i], input));
}
else{
cout << "The Key is not found" << endl;
}
}
for(int i = 0; i < hold.size(); i++)
{
cout << hold[i] << endl;
//Problem here:: I want to see the "data" that the search function returned not the hex value
}
}
Node * search(Node * x, const int & input)
{
if(x == NULL)
{
return NULL;
}
else
{
if(input == x->element)
{
return x;
}
search(x->left, input);
search(x->next, input);
}
}
我想,你的X->搜索()函數元素贏得的結果也不能編譯。正如扎克指出的那樣,你的編碼標準有點弱。但至少你在努力。隨着時間的推移,你會學會假設你堅持下去。 –