封装栈和队列

#include <iostream>

using namespace std;

template<class T>
class MyStack
{
private:
    int top;//栈顶
    int size;//栈的容量
    T* data;//数据
public:
    //无参构造
    MyStack()
    {
        this->top = -1;
        this->size = 0;
        this->data = nullptr;
    }
    //有参构造
    MyStack(int size)
    {
        this->top = -1;
        this->size = size;
        this->data = new T[size];
    }
    //析构
    ~MyStack()
    {
        if(this->data != nullptr)
        {
            delete []this->data;
        }
    }

    //判空
    bool empty()
    {
        return this->top == -1;
    }
    //判满
    bool full()
    {
        return this->top == this->size-1;
    }
    //入栈
    void push_stack(T data)
    {
        if(this->full())
        {
            cout << "栈已满,入栈失败" << endl;
        }else
        {
            this->data[++this->top] = data;
            cout << data << "    入栈成功" << endl;
        }
    }
    //出栈
    void pop_stack()
    {
        if(this->empty())
        {
            cout << "空栈,出栈失败" << endl;
        }else
        {
            cout<< "出栈成功    " << this->data[this->top--]<<endl;
        }
    }
};

测试代码:

int main()
{
    MyStack<int> st(10);
    for(int i=1; i<=11; i++)
    {
        st.push_stack(i);
    }

    for(int i=0; i<12; i++)
    {
        st.pop_stack();
    }
    return 0;
}

 结果:

1    入栈成功
2    入栈成功
3    入栈成功
4    入栈成功
5    入栈成功
6    入栈成功
7    入栈成功
8    入栈成功
9    入栈成功
10    入栈成功
栈已满,入栈失败
出栈成功    10
出栈成功    9
出栈成功    8
出栈成功    7
出栈成功    6
出栈成功    5
出栈成功    4
出栈成功    3
出栈成功    2
出栈成功    1
空栈,出栈失败
空栈,出栈失败

队列:

#include <iostream>

using namespace std;
template <typename T>
class MyQueue
{
private:
   int head;//头下标
   int tail;//尾下标
   int size;//容量
   T* data;//数据
public:
    //无参构造
    MyQueue()
    {
        this->head = 0;
        this->tail = 1;
        this->data = nullptr;
    }
    //有参构造
    MyQueue(int size)
    {
        this->head = 0;
        this->tail = 1;
        this->size = size;
        this->data = new T[size];
    }
    //析构
    ~MyQueue()
    {
        if(this->data != nullptr)
        {
            delete []this->data;
        }
    }

    //判空
    bool empty()
    {
        return (this->head+1)%this->size == this->tail;
    }
    //判满
    bool full()
    {
        return this->tail == this->head;
    }
    //入队
    void push(T& data)
    {
        if(this->full())
        {
            cout << "队已满,入队失败" <<endl;
        }else
        {
            this->data[this->tail] = data;
            this->tail = (this->tail+1)%size;
            cout << data << "    入队成功" << endl;
        }
    }
    //出队
    void pop()
    {
        if(this->empty())
        {
            cout << "空队列,出队失败" <<endl;
        }else
        {
             this->head = (this->head+1)%size;
            cout << this->data[this->head] << "    出队成功" << endl;
        }
    }
};

测试代码:

int main()
{
    MyQueue<int> mq(11);
    for(int i=1; i<=11; i++)
    {
        mq.push(i);
    }

    for(int i=0; i<11; i++)
    {
        mq.pop();
    }
    return 0;
}

测试结果:

1    入栈成功
2    入栈成功
3    入栈成功
4    入栈成功
5    入栈成功
6    入栈成功
7    入栈成功
8    入栈成功
9    入栈成功
10    入栈成功
栈已满,入栈失败
出栈成功    10
出栈成功    9
出栈成功    8
出栈成功    7
出栈成功    6
出栈成功    5
出栈成功    4
出栈成功    3
出栈成功    2
出栈成功    1
空栈,出栈失败
空栈,出栈失败

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傾语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值