雙指針鏈表單指針,相較於鏈表,二叉樹
1.1。這是我從教程中看到的,我只寫了重要的部分。
sortedInsert(Node **root, int key){}; int main(){ Node *root = &a; sortedInsert(&root, 4); }
1.2。不過,我只是使用指針而不是雙指針,並且一切正常,我可以成功地插入密鑰。
sortedInsert(Node *root, int key){}; int main(){ Node *root = &a; sortedInsert(root, 4); }
二叉樹
2.1。教程(雙指針)
void insert_Tree(Tree **root, int key){
}
int main(){
Tree *root = NULL;
insert_Tree(&root, 10);
}
2.2。我所做的是下面的,我沒有插入鑰匙,當我檢查了節點插入後,該節點仍然空。(單指針)
void insert_Tree(Tree *root, int key){
if(root == NULL){
root = (Tree *)malloc(sizeof(Tree));
root->val = key;
root->left = NULL;
root->right = NULL;
cout<<"insert data "<<key<<endl;
}else if(key< root->val){
insert_Tree(root->left, key);
cout<<"go left"<<endl;
}else{
insert_Tree(root->right, key);
cout<<"go right"<<endl;
}
}
int main(){
Tree *root = NULL;
insert_Tree(root, 10);
}
我有幾個問題
1) 。這是對的,1.1/2.1雙指針還是1.2/2.2單指針?請詳細解釋一下,如果你能舉一個例子,這可能會更好,我認爲他們都是對的。 2)。爲什麼我用單個指針成功地在關聯列表中插入關鍵字,但是我用單個指針插入樹失敗了?
非常感謝,我感謝大家的幫助。
嗨,亞當,你能告訴我爲什麼我們應該使用指針而不是指針?指針也設置或獲取地址的值,所以它應該保存所有的變化,對吧? – hellocoding