类模板的语法

定义一个类模板:

Template < class或者也可以用typename T >
class类名{
//类定义......
};

说明:其中,template是声明各模板的关键字,表示声明一个模板,模板参数可以是一个,也可以是多个。

例如

template <class Type>
class stack_class
{
    private:
        int max_len;//空间大小 
        int len;//当前元素个数 
        Type *bottom;//头指针 
        Type *top;//栈顶的指针 
        
    public:
        stack_class();
        stack_class(stack_class<Type> & arg);
        ~stack_class();
        bool IsEmpty();//是否为空,是返回1 
        void Push(const Type & arg);//栈插入元素 
        Type Pop();
};

 

template <class Key ,class Type>
class rbtree_class{
    private:
        node<Key,Type> *root;
        node<Key,Type> *nil;
        int size;
    public:
        rbtree_class();
        void NodeCpy(node<Key,Type>* &,node<Key,Type>* &,node<Key,Type>* & );
        rbtree_class(rbtree_class<Key,Type> &);
        void NodeDel(node<Key,Type> * );
        ~rbtree_class();
        //首先定义左旋转与右旋转 左旋把节点的右孩子(存在)转到它位置,自己变成其左孩子,右旋相反,后面分析要用到这两个操作 
        void LeftRotate(node<Key,Type> *); 
        void RightRotate(node<Key,Type> *);
        //然后定义fixup维持红黑树的性质
        void RbInsertFixup(node<Key,Type> * &);
        bool Insert(const Key &,const Type &);
        void Show(node<Key,Type> *);
        void ShowAll();//输出所有元素(按续)与颜色
        node<Key,Type> * MinNode(node<Key,Type> *);//从某节点开始向下找最小Key值的节点 
        node<Key,Type> * Min();
        node<Key,Type> * Replace(node<Key,Type> * des,node<Key,Type> * src);//src带上自己的子树替代des,父母节点相关的替代, 
        node<Key,Type> * SearchNode(node<Key,Type> *,const Key &);
        node<Key,Type> * Search(const Key &);//搜寻key,返回这个key的节点的指针 不存在返回nil
        void RbDeleteFixup(node<Key,Type> * &);
        bool Delete(const Key&);
         
        
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值