封装LIST

#include 

using namespace std;

template 

class MyVector
{

private:
    T *first;
    T *last; //最后一位数的下一位坐标
    T *end;  //整个容器的最后的下一位
public:
    //无参构造
  //MyVector():first(nullptr),last(nullptr),end(nullptr){}
    //有参构造
    MyVector(int size=2)
    {
        first=new T[size];
        last=first;
        end=first+size;
    }
    ~MyVector()
    {
        delete []first;
        cout<<"析构函数"<first=new T[size];
        memcpy(this->first,other.first,len*sizeof(T));
        this->last=this->first+len;
        this->end=this->first+size;
    }
    //判空
    bool empty()
    {
        return this->first==this->end;
    }
    //判满
    bool full()
    {
        return  this->last==this->end;
    }
    //扩容  初始size=2,所以2倍扩容
    void greater()
    {
        int size=this->end-this->first;
        T *newfirst=new T[size*2];
        memcpy(newfirst,this->first,sizeof(T)*(size));
        delete []first;
        first=newfirst;
        last=newfirst+size;
        end=newfirst+size*2;
    }
    //尾插
    void push_back(const T val)
    {
        if(full())
        {
            greater();
        }
        *(last)=val;
        last++;
    }
    //尾删
    void del_back()
    {
        if(empty())
        {
            cout<<"表已空,无法删除"<last;
        for(int i=0;ilast-this->first;i++)
        {
            *now=*(now-1);
            now--;
        }
        *first=val;
        last++;
    }
    //头删
    void del_head()
    {
        if(empty())
        {
            cout<<"表已空,无法删除"<last-this->first-1);i++)
        {
            *now=*(now+1);
            now++;
        }
        last--;
    }
    void show()
    {
        int len=last-first;
        T *now=first;
        for(int i=0;ilast-this->first;
    }
    //获取容量
    int size()
    {
        return this->end-this->first;
    }
};
int main()
{
    MyVector s1;
    s1.push_back(1);
    s1.push_back(2);
    s1.push_back(3);
    s1.push_head(4);
    s1.push_head(5);
    s1.push_head(6);
    s1.show();
    s1.del_back();
    s1.del_head();
    s1.show();
    cout<

封装LIST_第1张图片

你可能感兴趣的:(c++,c语言,算法)