8.30-C++-作业

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

实现stack栈

#include 

using namespace std;
template 
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<<"栈为空"<last--;
    }
    //访问栈顶元素
    T top()
    {
        if(this->empty())
        {
            cout<<"栈为空"<last-1);
    }

};

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

实现queue队列

#include 

using namespace std;
template 
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<<"队列为空"<first);
    }
    //访问最后一个元素
    T back()
    {
        return *(this->last-1);
    }
    int size()
    {
        return this->last-this->first;
    }

};

int main()
{
    Myqueue q;
    for(int i=1;i<=10;i++)
    {
        q.push(i);
    }
    q.pop();
    cout<<"第一个元素:"<

 

你可能感兴趣的:(c++,开发语言,蓝桥杯)