C++模板类

文章详细介绍了C++中使用模板类实现栈和队列的数据结构,包括构造函数、判断空/满、入栈/出栈、遍历、获取大小和清空操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、栈

#include <iostream>
#define MAX 8

using namespace std;

template <typename type>
class Shu
{
    type *arr;  //栈的数组
    int top;  //记录栈顶元素的下标

public:
    //构造函数
    Shu(){}

    //有参构造函数
    Shu(int t):top(t)
    {
        arr=new type[MAX];
    }
    
    //拷贝构造
    Shu(const Shu&other):top(other.top)
    {
        other.arr=new type[MAX];
    }

    //析构函数
    ~Shu()
    {
        delete []arr;
        arr=nullptr;
    }

    //判断空
    int stack_empty();

    //判断满
    int stack_full();

    //入栈
    int stack_push(type && data);

    //遍历栈
    void stack_show();

    //出栈
    int stack_pop();

    //获取栈顶元素
    int stack_top();

    //求栈的大小
    int stack_size();

    //清空栈
    void stack_free();

};

template <typename type>
//判断空
int Shu<type>::stack_empty()
{
    if(NULL==arr)
    {
        cout<<"判空失败"<<endl;
        return -1;
    }

    return this->top==-1;

}

template <typename type>
//判断满
int Shu<type>::stack_full()
{
    if(NULL==arr)
    {
        cout<<"判满失败"<<endl;
        return -1;
    }

    return this->top==MAX-1;
}

template <typename type>
//入栈
int Shu<type>::stack_push(type && data)
{
    if(stack_full())
    {
        cout<<"入栈失败"<<endl;
        return -1;
    }

    //top下标自增
    this->top++;

    //将输入的函数放入数组中
    arr[this->top]=data;

    cout<<"入栈成功"<<endl;

    return 0;
}

template <typename type>
//遍历栈
void Shu<type>::stack_show()
{
    if(stack_empty())
    {
        cout<<"遍历失败"<<endl;
        return ;
    }

    //遍历
    for(int i=0;i<this->top+1;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

template <typename type>
//出栈
int Shu<type>::stack_pop()
{
    if(stack_empty())
    {
        cout<<"出栈失败"<<endl;
        return -1;
    }

    //出栈
    cout<<arr[this->top]<<"出栈成功"<<endl;

    //自减
    this->top--;

    return 0;
}

template <typename type>
//获取栈顶元素
int Shu<type>::stack_top()
{
    if(stack_empty())
    {
        cout<<"获取栈顶元素失败"<<endl;
        return -1;
    }

    //获取栈顶元素
    cout<<"栈顶元素为:"<<arr[top]<<endl;

    return 0;
}

template <typename type>
//求栈的大小
int Shu<type>::stack_size()
{
    if(stack_empty())
    {
        cout<<"求栈大小失败"<<endl;
        return -1;
    }

    //栈的大小
    int size=this->top+1;

    cout<<"栈的大小为:"<<size<<endl;
    return 0;
}

template <typename type>
//清空栈
void Shu<type>::stack_free()
{
    this->top=-1;

    cout<<"清空栈成功"<<endl;
}

int main()
{
    Shu<int> zhan(-1);
    //入栈
    zhan.stack_push(2);
    zhan.stack_push(5);
    zhan.stack_push(3);
    zhan.stack_push(8);

    //遍历栈
    zhan.stack_show();

    //出栈
    zhan.stack_pop();
    zhan.stack_pop();

    //获取栈顶元素
    zhan.stack_top();

    //求栈的大小
    zhan.stack_size();

    //清空栈
    zhan.stack_free();

    return 0;
}

2、循环队列

#include <iostream>
#define MAX 8

using namespace std;

template <typename type>
class Shu
{
    type *arr;  //循环队列的数组
    int head;  //记录队列头元素的下标
    int tail;  //记录队列尾元素下标

public:
    //构造函数
    Shu(){}

    //有参构造
    Shu(int h,int t):head(h),tail(t)
    {
        arr=new type[MAX];
    }

    //拷贝构造
    Shu(const Shu&other):head(other.head),tail(other.tail)
    {
        other.arr=new type[MAX];
    }
    
    //析构函数
    ~Shu()
    {
        delete []arr;
        arr=nullptr;
    }

    //判断空
    int queue_empty();

    //判断满
    int queue_full();

    //入队
    int queue_push(type && data);

    //遍历队
    void queue_show();

    //出队
    int queue_pop();

    //求队列的大小
    int queue_size();

    //清空队
    void queue_free();

};


template <typename type>
//判断空
int Shu<type>::queue_empty()
{
    if(NULL==arr)
    {
        cout<<"判空失败"<<endl;
        return -1;
    }

    return this->head==this->tail;

}


template <typename type>
//判断满
int Shu<type>::queue_full()
{
    if(NULL==arr)
    {
        cout<<"判满失败"<<endl;
        return -1;
    }

    return (this->tail+1)%MAX==this->head;
}


template <typename type>
//入队
int Shu<type>::queue_push(type && data)
{
    if(queue_full())
    {
        cout<<"入队失败"<<endl;
        return -1;
    }

    //将输入的函数放入数组中
    arr[this->tail]=data;

    //尾下标自增
    this->tail=(this->tail+1)%MAX;

    cout<<"入队成功"<<endl;

    return 0;
}


template <typename type>
//遍历队
void Shu<type>::queue_show()
{
    if(queue_empty())
    {
        cout<<"遍历失败"<<endl;
        return ;
    }

    //遍历
    for(int i=this->head;i!=this->tail;i=(i+1)%MAX)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}


template <typename type>
//出队
int Shu<type>::queue_pop()
{
    if(queue_empty())
    {
        cout<<"出队失败"<<endl;
        return -1;
    }

    //出队
    cout<<arr[this->head]<<"出队成功"<<endl;

    //自增
    this->head=(this->tail+1)%MAX;

    return 0;
}


template <typename type>
//求队列的大小
int Shu<type>::queue_size()
{
    if(queue_empty())
    {
        cout<<"求队列大小失败"<<endl;
        return -1;
    }

    //队列的大小
    int size=(this->tail+MAX-this->head)%MAX;

    cout<<"队列的大小为:"<<size<<endl;
    return 0;
}


template <typename type>
//清空队
void Shu<type>::queue_free()
{
    this->head=0;
    this->tail=0;

    cout<<"清空队列成功"<<endl;
}

int main()
{
    Shu<int> duilie(0,0);
    //入队
    duilie.queue_push(2);
    duilie.queue_push(5);
    duilie.queue_push(3);
    duilie.queue_push(8);

    //遍历队
    duilie.queue_show();

    //出队
    duilie.queue_pop();
    duilie.queue_pop();

    //求队列的大小
    duilie.queue_size();

    //清空队
    duilie.queue_free();

    return 0;
}

3、思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值