双指针链表单指针,相较于链表,二叉树
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