【发布时间】:2013-08-26 04:49:31
【问题描述】:
-
对于单链表
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)。为什么单指针链表插入key成功,单指针树插入失败?
非常感谢,感谢大家的帮助。
【问题讨论】:
标签: pointers linked-list binary-tree insertion double-pointer