C++栈和队列的容器实现

栈的容器实现

代码展示

#include <iostream>

using namespace std;
template<typename T>
class Stack
{
private:
    int top;
    T*bottom;
    T*end;

public:
    Stack(int size = 2) {
        bottom = new T[size];//栈底指针
        top = -1;
        end = bottom+size;//栈空间尾指针

    }
    //析构函数
    ~Stack()
    {
        delete[]bottom;
        end=bottom= NULL;
    }
    //拷贝构造函数
    Stack(const Stack &other)
    {
        //先计算出原空间的尺寸
        int lenth =top;
        int size = other.end - other.bottom;
        this->bottom = new T[size];
        memcpy(this->bottom,other.bottom,lenth*sizeof(T));
        //将该对象的另外两个指针指向对应位置
        this->end = this->bottom+size;
        this->top = this->top+size;

    }
    //判空
    bool empty()
    {
        return top == -1;

    }
    //判满
    bool full()
    {
        return this->top == this->size()-1;
    }
    void greater()
    {
        //获取当前容器总容量
        int size = this->end-this->bottom;
        //在堆区重新申请一个2倍的空间
        T*temp = new T[2*size];
        //将原空间数据放到新申请的空间中
        memcpy(temp,this->bottom,size*sizeof(T));
        //释放原来的空间
        delete []bottom;
        //更新指针指向
        bottom = temp;
        //更新其他指针
        end = bottom + 2*size;
    }
    //实现尾插
    void push_back(const T val)
    {
        if(this->full())
        {
            this->greater();//如果满了,进行扩容
        }
        top++;
        bottom[top] = val;
    }
    //实现尾删
    void pop_back()
    {
        if(this->empty())
        {
            return;
        }
        --top;
    }
    //实现获取第一个元素
    T fornt()const
    {
        return *bottom;
    }

    int size()
    {
        return end-bottom;
    }

    int lenth()
    {
        return top;
    }

    T &at(int index)
    {
        if((index<0) || index > this->lenth())
        {
            cout<<"访问越界"<<endl;
        }
        return bottom[index];
    }
};


int main()
{
    Stack<int>v;
    for(int i=1; i<=20; i++)
        {
            v.push_back(i);
            cout<<v.size()<<endl;
        }

        for(int i=0; i<20; i++)
        {
            cout<<v.at(i)<<" ";
        }
        cout<<endl;
    cout << "Hello World!" << endl;
    return 0;
}


运行结果

在这里插入图片描述

队列的容器实现

代码展示

#include <iostream>
#include <cstring>

using namespace std;

template<class T>
class myqueue {
private:
    T*first;//空间数组头指针
    int front;//队列头下标
    int tail;//队列尾下标
    int size;

public:
    //构造函数
    myqueue()
    {
        size=20;
        front=tail=0;
        first = new T[size];
    }
    //析构函数
    ~myqueue()
    {
        delete[]first;
        first=NULL;
    }

    //拷贝构造函数
    myqueue(const myqueue &other)
    {


        //先计算出原空间的尺寸
        int len = (tail+size-front)%size;

        this->first = new T[size];      //新对象的容量

        memcpy(this->first, other.first, len*sizeof(int));


    }

    //判空
    bool empty()
    {
        return this->tail == this->front;
    }
    //判满
    bool full()
    {
        return (this->tail+1)%size == this->front;
    }

    //扩容原则:2倍扩容
    void greater()
    {


        //在堆区重新申请一个2倍的空间
        T *temp = new T[2*size];
        size=size*2;

        //将原空间数据放到新申请的空间中
        memcpy(temp, this->first, size*sizeof (int));

        //是否原来的空间
        delete []first;

        //更新指针指向
        first = temp;

    }

    //实现入队
    void push_back( const T val)
    {
        //判断是否已经满了
        if(this->full())
        {
            this->greater();    //如果满了,进行扩容
        }
        else
        {

         tail=(tail+1)%size;
         first[tail]=val;
        }
    }
    //实现出队
    void pop_back()
    {
        if(this->empty())
        {
            return;
        }

        //尾指针前移
        front=(front+1)%size;
    }

    //实现获取第一个元素
    T mfront() const
    {
        return first[front];
    }

    //求总容量
    int msize()
    {
        return (tail+size-front)%size;
    }

    //求使用的容量
    int len()
    {
        return size;
    }

    //返回当前元素的引用
    T &at(int index)
    {
        if(index<0 || index>(index+1)%size)
        {
            cout<<"访问越界"<<endl;
        }

        return  first[index];
    }

};

int main()
{

    myqueue<int> v;

    //cout<<v.size()<<endl;
    for(int i=1; i<=20; i++)
    {
        v.push_back(i);
        cout<<v.msize()<<endl;
    }

    for(int i=1; i<20; i++)
    {
        cout<<v.at(i)<<" ";
    }
    cout<<endl;

    return 0;
}

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值