模板 vector list

vector
#define _CRT_SECURE_NO_DEPRECATE 1
#include<iostream>
#include<assert.h>
using namespace std;
template<class T>
class Vector
{
public:
    Vector()
        :_data(new T[3])
        ,_size(0)
        ,_capacity(3)
    {}
    Vector(size_t n, const T& data)
        :_data(new T[n])
    {
        for(size_t i=0; i<n; i++)
        {
            _data[i] = data;
        }
        _size = n;
        _capacity = n;
    }
    Vector(const Vector& d)
        :_data(new T[d._capacity])
    {
        for(size_t i=0; i<d._size; i++)
        {
            _data[i] = d._data[i];
        }
        _size = d._size;
        _capacity = d._capacity;

    }
    Vector& operator=(const Vector& d)
    {
        if(_data != d._data)
        {
            Vector tmp(d);
            swap(d._data, _data);
        }
        return *this;
    }
    ~Vector()
    {
        delete[] _data;
        _data = NULL;
        _size = 0;
        _capacity = 0;
    }
    void Swap(Vector& v) 
    { 
        std::swap(_pData, v._pData); 
        std::swap(_capacity, v._capacity); 
        std::swap(_size, v._size); 
    } 


    void PushBack(const T data)
    {
        //查满
        //没有元素
        //有元素
        CheckCapacity();
        _data[_size++] = data;
    }
    void PopBack()
    {
        assert(_size>0);
        _size--;
    }
    void Insert(size_t pos, const T& data)
    {
        //判断pos是否正确
        //查满
        //pos位置开始的数后移
        //在pos上插入data
        assert(pos<_size);
        CheckCapacity();
        int tmp =(int)_size;      //有缺陷
        while ((int)pos <= tmp)   //无符号数0位置的问题  
        {
            _data[tmp] = _data[tmp-1];
            tmp--;
        }
        _data[pos] = data;
        ++_size;

    }
    void Erase(size_t pos)
    {
        //判断pos位置
        //从pos后依次往前覆盖
        assert(pos < _size);
        size_t tmp = pos;

            while (tmp < _size)
            {
                _data[tmp] = _data[tmp+1];
                tmp++;
            }
            _size--;
    }
    void Assign(size_t n, const T& data)
    {
        //功能:新的内容是n个元素,每个元素都初始化为val。
        assert(n>0);
        if(n<=_capacity)
        {
            size_t tmp = 0;
            while (tmp<n)
            {
                _data[tmp] = data;
                tmp++;
            }
        }
        else
        {
            delete[]_data;
            _data = new T[n];
            size_t tmp = 0;
            while (tmp<n)
            {
                _data[tmp] = data;
                tmp++;
            }
            _capacity = n;
        }

            _size = n;
    }
    void Clear()
    {
        _size = 0;
    }
    T& operator[](size_t index)
    {
        assert(index < _size);
        return _data[index];
    }
    const T& operator[](size_t index)const
    {
        assert(index < _size);
        return _data[index];
    }
    T& Front()
    {
        assert(_size>0);
        return _data[0];
    }
    const T& Front()const
    {
        assert(_size>0);
        return _data[0];
    }
    T& Back()
    {
        assert(_size>0);
        return _data[_size-1];
    }
    const T& Back()const
    {
        assert(_size>0);
        return _data[_size-1];
    }

    void PrintVector()
    {
        size_t i = 0;
        while(i<_size)
        {
            cout<< _data[i]<<"  " ;
            ++i;
        }
        cout<<"\n";
        cout<<"_size:"<<_size<<endl;
        cout<<"_capacity:"<<_capacity<<endl;

    }
    size_t Size()const
    {
        return _size;
    }
    size_t Capacity()const
    {
        return _capacity;
    }
    bool Empty()const
    {
        return !_size;
    }
    void ReSize(size_t sz, const T& data = T())
    {
        //3种情况
        if(sz == _size)
        {
            return ;
        }
        if(sz <_size)
        {
            _size = sz; 
        }
        else if(sz>_size && sz< _capacity)
        {
            size_t tmp = _size;
            while(tmp<sz)
            {
                _data[tmp] = data;
                tmp++;
            }
            _size = sz;
        }else
        {
            T* tmp = new T[sz];
            size_t i=0;
            while(i<_size)
            {
                tmp[i]=_data[i];
                i++;
            }
            while(i<sz)
            {
                tmp[i]=data;
                i++;
            }
            delete[]_data;
            _data = tmp;
            _size = sz;
            _capacity = sz;
        }

    }
    //只改变容量
    void Reserve(size_t sz)
    {
        if(sz>_capacity)
        {
            T* tmp = new T[sz];
            size_t i=0;
            while(i<_size)
            {
                tmp[i]=_data[i];
                i++;
            }

            delete[]_data;
            _data = tmp;
            _capacity = sz;
        }
    }

private:
    void CheckCapacity()
    {
        //满
        //不满
        if(_size == _capacity)
        {
            T* tmp = new T[_capacity+3];
            assert(tmp);
            for(size_t i=0; i<_size; i++)
            {
                tmp[i] = _data[i];
            }
            delete[] _data;
            _data = tmp;
            _capacity += 3;
        }


    }
private:
    T* _data;
    size_t _size;
    size_t _capacity;
};

void test1()
{
    Vector<int> d1;
    d1.PushBack(1);
    d1.PushBack(2);
    d1.PushBack(3);
    d1.PushBack(4);

    d1.PopBack();
    d1.PopBack();
    d1.PopBack();
    d1.PopBack();



    Vector<char> d2;
    d2.PushBack('a');
    d2.PushBack('b');
    d2.PushBack('c');
    d2.PushBack('d');

    d2.PopBack();
    d2.PopBack();
    d2.PopBack();
    d2.PopBack();
}
void test2()
{
    Vector<int> d3(5,1);
    d3.PrintVector();

    d3.Insert(0,0);
    d3.PrintVector();

    d3.Erase(0);
    d3.PrintVector();

    d3.ReSize(10,0);
    d3.PrintVector();

    d3.Reserve(15);
    d3.PrintVector();

    d3.Assign(20,5);
    d3.PrintVector();

}
int main()
{
    test2();
    system("pause");
    return 0;
}
list
#define _CRT_SECURE_NO_DEPRECATE 1
#include<iostream>
#include<assert.h>
using namespace std;

template<class T>
struct ListNode
{
    ListNode(const T& data = T())
        :_pre(NULL)
        ,_next(NULL)
        ,_data(data)
    {
    }
    ListNode<T>* _pre;
    ListNode<T>* _next;
    T _data;
};


template<class T>
class ListItertor
{
typedef ListItertor<T> itor;
public:
    ListItertor(ListNode<T>* p = NULL)
        :_pNode(p)
    {
    }

    itor& operator++()
    {
        _pNode = _pNode->_next;
        return *this;
    }

    itor operator++(int) 
    { 
        itor temp(*this); 
        _pNode = _pNode->_next; 
        return temp; 
    } 


    itor& operator--()
    {
        _pNode = _pNode->_pre;
        return *this;
    }

    itor operator--(int)
    {
        itor tmp(*this);
        _pNode = _pNode->_pre;
        return tmp;
    }

    bool operator==(const itor& d)
    {
        return _pNode == d._pNode;
    }

    bool operator!=(const itor& d)
    {
        return _pNode != d._pNode;
    }


private:
    ListNode<T>* _pNode;
};
template<class T>
class List
{
typedef ListItertor<T> itor;
public:
    ListNode<T>* Begin()
    {
        return (_pHead->_next);  //构造函数返回?
    }
    ListNode<T>* End()
    {
        return (_pHead);
    }
    List()
    {
        CreateHead();
    }
    List(size_t n, const T& data)
    {
        CreateHead();
        while (n-->0)
        {
            Insert(End(),data);
        }
    }

    List(const List<T>& L)
    {
        CreateHead();
        ListNode<T>* tmp = L._pHead->_next;
        while(tmp != L._pHead) //cuowu
        {
            Insert(End(),tmp->_data);
            tmp = tmp->_next;
        }
    }
    void Swap(ListNode<T>** p1,ListNode<T>** p2)
    {
        ListNode<T>* tmp = *p1;
        *p1 = *p2;
        *p2 = tmp;

    }
    List& operator=(const List<T>& L)
    {
        if(_pHead != L._pHead)
        {
            List<T> tmp(L);
            Swap(&_pHead,&(tmp._pHead));
        }
        return *this;
    }
    void Clear()
    {
        ListNode<T>* cur = Begin();
        while(cur != End())
        {
            ListNode<T>* tmpNode = cur;
            cur = cur->_next;
            delete tmpNode;
            tmpNode = NULL;
        }
    }
    ~List()
    {
        Clear();
        delete _pHead;
        _pHead = NULL;
    }
    void PushBack(const T& data)
    {
        Insert(End(),data);
    }
    void PopBack()
    {
        ListNode<T>* tmp = End();
        Erase(tmp->_pre);//--End()
    }
    void PushFront(const T& data)
    {
        Insert(Begin(),data);
    }
    void PopFront()
    {
        Erase(Begin());
    }
    void Insert(ListNode<T>* pos, const T& data)     //在pos前插入
    {
        assert(pos);
        ListNode<T>* pNewNode = new ListNode<T>(data);//标准插入顺序
        pNewNode->_next = pos;
        pos->_pre->_next = pNewNode;
        pNewNode->_pre = pos->_pre;
        pos->_pre = pNewNode;
    }
    void Erase(ListNode<T>* pos)                    //删除pos处节点
    {
        assert(pos);
        pos->_next->_pre = pos->_pre;  //cuowu
        pos->_pre->_next = pos->_next;
        delete pos;
        pos = NULL;
    }
    size_t Size()
    {
        size_t count = 0;
        ListNode<T>* cur = _pHead->_next;
        while(cur != End())
        {
            count++;
            cur=cur->_next;
        }
        return count;
    }
    bool Empty()
    {
        if(_pHead->_next == _pHead->_pre)
            return true;
        return false;
    }
    void PrintList()
    {
        if(_pHead->_next != _pHead->_pre)
        {
            ListNode<T>* tmp = Begin();
            while (tmp!=End())
            {
                cout<<tmp->_data<<" ";
                tmp = tmp->_next;
            }
            cout<<endl;
        }
    }

private:
    void CreateHead()
    {
        _pHead = new ListNode<T>();
        _pHead->_next = _pHead;
        _pHead->_pre = _pHead;
    }
private:
    ListNode<T>* _pHead;
};
void test1()
{

    List<int> L1(2,5);    //构造
    L1.PrintList();

    List<int> L2(L1);     //拷贝构造
    L2.PrintList();

    List<int> L3;         //赋值运算符
    L3.PushBack(6);       //Insert
    L3.PushBack(6);
    L3.PrintList();

    L3=L2;
    L3.PrintList();
}

void test2()
{
    List<int> L1;       
    L1.PushBack(1);         
    L1.PushBack(2);
    L1.PushBack(3);
    L1.PushBack(4);
    L1.PushBack(5);
    L1.PrintList();

    L1.PopBack();          //Erase
    L1.PopBack();
    L1.PopBack();
    L1.PopBack();
    L1.PopBack();
    L1.PrintList();


    L1.PushFront(6);
    L1.PushFront(5);
    L1.PushFront(4);
    L1.PushFront(3);
    L1.PushFront(2);
    L1.PushFront(1);
    L1.PrintList();

    cout<<L1.Size()<<endl;             //Size

    L1.PopFront();
    L1.PopFront();
    L1.PopFront();
    L1.PopFront();
    L1.PopFront();
    L1.PopFront();
    L1.PrintList();
    cout<<L1.Empty()<<endl;            //Empty

}
int main()
{

    test2();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值