C++模板类

1、栈

#include 
#define MAX 8

using namespace std;

template 
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 
//判断空
int Shu::stack_empty()
{
    if(NULL==arr)
    {
        cout<<"判空失败"<top==-1;

}

template 
//判断满
int Shu::stack_full()
{
    if(NULL==arr)
    {
        cout<<"判满失败"<top==MAX-1;
}

template 
//入栈
int Shu::stack_push(type && data)
{
    if(stack_full())
    {
        cout<<"入栈失败"<top++;

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

    cout<<"入栈成功"<
//遍历栈
void Shu::stack_show()
{
    if(stack_empty())
    {
        cout<<"遍历失败"<top+1;i++)
    {
        cout<
//出栈
int Shu::stack_pop()
{
    if(stack_empty())
    {
        cout<<"出栈失败"<top]<<"出栈成功"<top--;

    return 0;
}

template 
//获取栈顶元素
int Shu::stack_top()
{
    if(stack_empty())
    {
        cout<<"获取栈顶元素失败"<
//求栈的大小
int Shu::stack_size()
{
    if(stack_empty())
    {
        cout<<"求栈大小失败"<top+1;

    cout<<"栈的大小为:"<
//清空栈
void Shu::stack_free()
{
    this->top=-1;

    cout<<"清空栈成功"< 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;
}

C++模板类_第1张图片

2、循环队列

#include 
#define MAX 8

using namespace std;

template 
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 
//判断空
int Shu::queue_empty()
{
    if(NULL==arr)
    {
        cout<<"判空失败"<head==this->tail;

}


template 
//判断满
int Shu::queue_full()
{
    if(NULL==arr)
    {
        cout<<"判满失败"<tail+1)%MAX==this->head;
}


template 
//入队
int Shu::queue_push(type && data)
{
    if(queue_full())
    {
        cout<<"入队失败"<tail]=data;

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

    cout<<"入队成功"<
//遍历队
void Shu::queue_show()
{
    if(queue_empty())
    {
        cout<<"遍历失败"<head;i!=this->tail;i=(i+1)%MAX)
    {
        cout<
//出队
int Shu::queue_pop()
{
    if(queue_empty())
    {
        cout<<"出队失败"<head]<<"出队成功"<head=(this->tail+1)%MAX;

    return 0;
}


template 
//求队列的大小
int Shu::queue_size()
{
    if(queue_empty())
    {
        cout<<"求队列大小失败"<tail+MAX-this->head)%MAX;

    cout<<"队列的大小为:"<
//清空队
void Shu::queue_free()
{
    this->head=0;
    this->tail=0;

    cout<<"清空队列成功"< 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;
}

C++模板类_第2张图片

3、思维导图

你可能感兴趣的:(c++,算法,开发语言)