欢迎使用CSDN-markdown编辑器

数据结构学习笔记

第一章 预备知识

1.2 函数与参数

1.2.1 传值参数
int Abc(int a,int b,int c){
    return a+b+c
    }
int main(){
z =Abc(2,x,y)
}
a,b,c是函数Abc的形式参数
2,x,y是对应于abc的实际参数
传值时实际参数通过复制构造函数将实际值复制给形式参数

1.2.2 模板函数
template<class T>
T Abc(T a,T b,T c){
    return a+b+c
}

1.2.3 引用参数
形式参数的用法会增加程序的运行开销
template<class T>
T Abc(T& a,T& b,T& c){
    return a+b+c
}

1.2.4 常量引用参数
template<class T>
T Abc(const T& a,const T& b,const T& c){
    return a+b+c
}
const可以致命函数不可以修改引用参数的值

1.2.5 返回值
T& X(int i,T& z){
    return z;
}
函数直接引用返回也可以减少开销

1.2.6 递归函数
阶乘函数f(n) =n!
f(n)= 1        (n<=1)
f(n)=nf(n-1)   (n>1)
斐波那契数列
F0=0,F1=1,Fn=F(n-1)+F(n-2)

计算n!的递归:
int Factorial(){
if(n<=1) return 1;
else return n*Factorial(n-1);
}
累加a[0:n-1]
    template<class T>
    T sum(T a[],int n){
        T tsum =0;
        for(int i=0;i<n;i++){
            tsum += a[i];
        }
        return tsum;
    }


递归计算a[0:n-1]
template<class T>
T Rsum(T a[],int n){
    if(n>0)
        return Rsum(a,n-1) +a[n-1];
    return 0;
    }

//使用递归生成排列,这段代码输出所有前缀为list[0:k-1],后缀为list[k:m]的排列方式
template<class T>
void Perm(T list[],int k,int m){
    int i;
    if(k==m){
        for{i=0;i<=m;i++}
            cout<<list[i];
        cout<<endl;
    }
    else 
        for(i=k;i<=m;i++){
            Swap(list[k],list[i]);
            Perm(list,k+1,m);
            Swap(list[k],list[i]);
        }   
}

template<class T>
inline void Swap(T& a, T& b){
    T temp =a;
    a =b;
    b =temp;
}

1.3 动态分配内存

1.3.1 操作符new
int *y 
y=new int;
*y =10;
可以合并为:
int *y = new int (10);
int *y;
y=new int(10);
1.3.2 一维数组
float *x =new flost[n];
1.3.3 异常处理
1.3.4 操作符delete
delete y;//释放*y空间
delete []x;//释放一维数组x
1.3.5 二维数组
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值