c++实现栈和队列的操作

#include <iostream>
#include <stack>

using namespace std;

template <typename  T>
class mysta
{
private:
    T * front;
    T * end;
    int len;
public:
    //构造
    mysta(int size=20){
        front = new T[size];
        end=front;
        len=0;
    }
    //析构
    ~mysta()
    {
        delete []front;
        front=end=nullptr;
    }
    //拷贝构造函数
    mysta(const mysta &other)
    {
        int len=other.end-other.front;
        this->front=new T[20];
        memcpy(this->front,other.front,20*sizeof(T));
        this->end=this->front+len;
    }
    //判空
    bool empty()
    {
        return this->front == this->end;
    }

    //判满
    bool full()
    {
        return len>=20?true:false;
    }
    //进栈
    void push_back(const T val)
    {
        if(this->full())
        {
            cout<<"stark is full"<<endl;
            return;
        }
        *end=val;
        end++;
        len++;
    }

    //出栈
    void pop_back()
    {
        if(this->empty())
        {
            return;
        }
        --end;
        len--;
    }
    //访问栈顶元素
    T top() const
    {
        return *(end-1);
    }
    //计算元素个数
    int size()
    {
        return len;
    }
};

int main()
{
    mysta<int> s1;
    s1.push_back(5);
    s1.push_back(4);
    s1.push_back(3);
    cout<<"size="<<s1.size()<<endl;
    cout<<s1.top()<<endl;
    s1.pop_back();
    cout<<"size="<<s1.size()<<endl;
    s1.push_back(6);
    cout<<s1.top()<<endl;

    mysta<int> s2=s1;
    cout<<s2.top()<<endl;
    s2.pop_back();
    cout<<s2.top()<<endl;


    cout << "Hello World!" << endl;
    return 0;
}
#include <iostream>
#include <stack>

using namespace std;

template <typename  T>
class mysta
{
private:
    int front;
    int end;
    int len;
    T queue[20];
public:
    //构造
    mysta(){len=end=front=0;}
    //析构
    ~mysta(){}
    //拷贝构造函数
    mysta(const mysta &other)
    {
       this->end=other.end;
       this->front=other.front;
       this->len=other.len;
       memcpy(this->queue,other.queue,sizeof(other.queue));
    }
    //判空
    bool empty()
    {
        return this->front == this->end;
    }

    //判满
    bool full()
    {
        return (end+1)%20==front;
    }
    //进队列
    void push_back(const T val)
    {
        if(this->full())
        {
            cout<<"queue is full"<<endl;
            return;
        }
        queue[end]=val;
        end=(end+1)%20;
        len++;
    }

    //出队列
    void pop_back()
    {
        if(this->empty())
        {
            return;
        }
       front=((++front)%20);
        len--;
    }
    //访问第一个元素
    T top() const
    {
        return queue[front];
    }
    T tail() const
    {
        return queue[(end+19)%20];
    }
    //计算元素个数
    int size()
    {
        return len;
    }
};

int main()
{
    mysta<int> s1;
    s1.push_back(5);
    s1.push_back(4);
    s1.push_back(3);
    cout<<"size="<<s1.size()<<endl;  //3
    cout<<s1.top()<<endl;  //5
    cout<<s1.tail()<<endl;  //3
    s1.pop_back();
    cout<<"size="<<s1.size()<<endl;   //2
    s1.push_back(6);
    cout<<s1.top()<<endl;   //4
     cout<<s1.tail()<<endl;  //6

    mysta<int> s2=s1;
    cout<<s2.top()<<endl;    //4
    s2.pop_back();
    cout<<s2.top()<<endl;   //3


    cout << "Hello World!" << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值