关于函数传参的再理解(含引用)

情景引入—小小日记

6/1的我开开心心的度过儿童节,复习一哈数据结构课上的内容。
在这里插入图片描述
but!,这红红的一条让我心慌。不可创建变量!
下面是当时的源码(二叉排序树)

#include <iostream>
using namespace std;
typedef int datatype;
struct  Node{
    datatype data =0;
    Node *lchild = NULL, *rchild = NULL;
};


void Insert(Node *root,datatype data){
    Node *p=NULL;
    if(root==NULL){
        p= (Node *)malloc(sizeof(Node));
        p->data=data;
        p->lchild=NULL;
        p->rchild=NULL;
        root=p;
    }else
    if(data < root->data){
        Insert(root->lchild,data);
    }else{
        Insert(root->rchild,data);
    };
    return ;
}

void Print(Node *root){
    if(root==NULL)return;
    Print(root->lchild);
    cout<<root->data;
    Print(root->rchild);

}
datatype  a[100];
int main(){//Binary_Search_Tree
    int n;
    datatype data;
    Node * BST=NULL;

    cout<<"输入插入个数"<<endl;
    cin>>n;
    //datatype a[n];
    cout<<"输入插入数据"<<endl;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
//63 90 70 55 67 42 98
    for(int i=0;i<n;i++){
        Insert(BST,a[i]);
    }
    
    Print(BST);
    return 0;
}

聪明的你能发现错误吗?

#include<iostream>
using namespace std;
void f1(int *c,int d){
	cout<<*c<<endl;
	cout<<c<<endl;
	cout<<&c<<"    #将b所指的变量地址 赋给 新的指针变量c 使c指向a#"<<endl;
	cout<<endl;
	//cout<<d<<endl;
	//cout<<&d<<endl;
	//调用函数完成后会把c销毁 
}
void f2(int *&c,int d){
	cout<<*c<<endl;
	cout<<c<<endl;
	cout<<&c<<"    #引用原b地址#"<<endl;
	cout<<endl;
	//cout<<d<<endl;
	//cout<<&d<<endl;
	//此时c即是b的别名,修改c即是修改b 
}
int main(){
	int a=1;
	int *b=&a;
	cout<<a<<endl;
	cout<<&a<<"	#&a#"<<endl;
	cout<<endl;
	
	cout<<*b<<"		#a#"<<endl;
	cout<<b<<"	#&a#"<<endl;
	cout<<&b<<"	#&b#"<<endl;
	cout<<endl;
	
	f1(b,a);
	f2(b,a);
	return 0;
}

这下终于搞懂了吧,所以二叉排序树的的插入有以下两种写法:
1.这种写法好理解些,但是申请了临时结构体指针变量root

#include <iostream>
using namespace std;
typedef int datatype;
//63 90 70 55 67 42 98
struct  Node{
    datatype data;
    Node *lchild, *rchild;
};
Node *BST=NULL;

Node* Insert(Node *root,datatype data){
    Node *p=NULL;
    if(root==NULL){
        p= (Node *)malloc(sizeof(Node));
        p->data=data;
        p->lchild=NULL;
        p->rchild=NULL;
        root=p;
    }
	else if(data < root->data){
        root->lchild=Insert(root->lchild,data);
    }else{
        root->rchild=Insert(root->rchild,data);
    }
    return root;
}

void Print(Node *root){
    if(root==NULL)return;
    Print(root->lchild);
    cout<<root->data<<" ";
    Print(root->rchild);

}
datatype  a[100];
int main(){//Binary_Search_Tree
    int n;
    //datatype data;
    
    cout<<"输入插入个数"<<endl;
    cin>>n;
    //datatype a[n];
    cout<<"输入插入数据"<<endl;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<n;i++){
        BST=Insert(BST,a[i]);
    }
    Print(BST);
    return 0;
}

2.此写法简单但是对*&root要理解为传入指针的引用。

#include <iostream>
using namespace std;
typedef int datatype;
//63 90 70 55 67 42 98
struct  Node{
    datatype data;
    Node *lchild, *rchild;
};
Node *BST=NULL;

void Insert(Node *&root,datatype data){
    Node *p=NULL;
    if(root==NULL){
        p= (Node *)malloc(sizeof(Node));
        p->data=data;
        p->lchild=NULL;
        p->rchild=NULL;
        root=p;
    }
	else if(data < root->data){
        Insert(root->lchild,data);
    }else{
    	Insert(root->rchild,data);
    }

}

void Print(Node *root){
    if(root==NULL)return;
    Print(root->lchild);
    cout<<root->data<<" ";
    Print(root->rchild);

}
datatype  a[100];
int main(){//Binary_Search_Tree
    int n;
    
    cout<<"输入插入个数"<<endl;
    cin>>n; 
    cout<<"输入插入数据"<<endl;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<n;i++){
        Insert(BST,a[i]);
    }
    Print(BST);
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值