8.30-C++-作业

自己实现容器适配器的stack栈、queue队列

实现stack栈

#include <iostream>

using namespace std;
template <class T>
class Mystack
{
private:
    T* first;
    T* last;
    T* end;;
public:
    //构造函数
    Mystack()
    {
        first = new T[1];
        last = first;
        end = first+1;
    }
    //析构函数
    ~Mystack()
    {
        delete []first;
        first = last = end;
    }
    //返回容纳的元素数
    int size()const
    {
        return this->last-this->first;
    }
    //拷贝构造函数
    Mystack(const Mystack& other)
    {
        int n = other.end-other.first;
        this->first = new T[sizeof(n)];
        memcpy(this->first,other.first,n*sizeof(T));

        this->last = this->first + other.size();
        this->end = this->first + n;
    }
    //拷贝赋值函数
    Mystack& operator=(const Mystack& other)
    {
        int n = other.end-other.first;
        this->first = new T[sizeof(n)];
        memcpy(this->first,other.first,n*sizeof(T));

        this->last = this->first + other.size();
        this->end = this->first + n;
        return *this;
    }
    //判空
    bool empty()
    {
        return this->first == this->last;
    }
    bool full()
    {
        return this->end == this->last;
    }
    //扩容
    void greate()
    {
        int n = this->end-this->first;
        T* temp = new T[2*n];
        memcpy(temp,this->first,n*sizeof(T));
        delete []first;
        this->first = temp;
        this->last = this->first+n;
        this->end = this->first+2*n;

    }
    //向栈顶插入元素
    void push(const T val)
    {
        if(this->full())
        {
           this->greate();
        }
        *last = val;
        last++;
    }
    //删除栈顶元素
    void pop()
    {
        if(this->empty())
        {
            cout<<"栈为空"<<endl;
            return;
        }
        this->last--;
    }
    //访问栈顶元素
    T top()
    {
        if(this->empty())
        {
            cout<<"栈为空"<<endl;
            return (T)-1;
        }
        return *(this->last-1);
    }

};

int main()
{
    Mystack<int> s;
    for(int i=1;i<=10;i++)
    {
        s.push(i);
    }
    cout<<s.top()<<endl;

    s.pop();

    cout<<s.top()<<endl;
    cout<<"s.size="<<s.size()<<endl;

    Mystack<int> s2 = s;
    Mystack<int> s3;
    s3 = s;
    cout<<"s2:"<<s2.top()<<endl;
    cout<<"s3:"<<s3.top()<<endl;
    return 0;
}

实现queue队列

#include <iostream>

using namespace std;
template <class T>
class Myqueue
{
private:
    T* first;
    T* last;
    T* end;;
public:
    //构造函数
    Myqueue()
    {
        first = new T[1];
        last = first;
        end = first+1;
    }
    //析构函数
    ~Myqueue(){}
    //拷贝构造函数
    Myqueue(const Myqueue& other)
    {
        int n = other.end-other.first;
        this->first = new T[sizeof(n)];
        memcpy(this->first,other.first,n*sizeof(T));

        this->last = this->first + other.size();
        this->end = this->first + n;
    }
    //拷贝赋值函数
    Myqueue& operator=(const Myqueue& other)
    {
        int n = other.end-other.first;
        this->first = new T[sizeof(n)];
        memcpy(this->first,other.first,n*sizeof(T));

        this->last = this->first + other.size();
        this->end = this->first + n;
        return *this;
    }
    //判空
    bool empty()
    {
        return this->first == this->last;
    }
    //判满
    bool full()
    {
        return this->end == this->last;
    }
    //扩容
    void greate()
    {
        int n = this->end-this->first;
        T* temp = new T[2*n];
        memcpy(temp,this->first,n*sizeof(T));
        delete []first;
        this->first = temp;
        this->last = this->first+n;
        this->end = this->first+2*n;
    }
    //向队列尾部插入元素
    void push(const T val)
    {
        if(this->full())
        {
           this->greate();
        }
        *last = val;
        last++;
    }
    //删除首个元素
    int pop()
    {
        if(this->empty())
        {
            cout<<"队列为空"<<endl;
            return -1;
        }
        delete first;
        first++;
        return 0;
    }
    //访问第一个元素
    T front()
    {
        return *(this->first);
    }
    //访问最后一个元素
    T back()
    {
        return *(this->last-1);
    }
    int size()
    {
        return this->last-this->first;
    }

};

int main()
{
    Myqueue<int> q;
    for(int i=1;i<=10;i++)
    {
        q.push(i);
    }
    q.pop();
    cout<<"第一个元素:"<<q.front()<<endl;
    cout<<"最后一个元素:"<<q.back()<<endl;
    cout<<q.size()<<endl;

    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值