类模版实现动态顺序表

#include<iostream>
#include<assert.h>
using namespace std;
#pragma warning(disable:4996)

template<class T, int DefaultCapacity = 10>
class SeqList
{
public:
    SeqList(T* data = NULL, size_t size = 0);
    SeqList(const SeqList& s);
    ~SeqList();
public:
    void CheckCapacity();
    void PushBack(const T& x);
    void PopBack();
    void PushFront(const T& x);
    void PopFront();
    void Print();
    void Insert(size_t index, const T& x);
    void Modified(size_t index, const T& x);
    int Find(const T& x);
    void Remove(size_t index);
private:
    T* _data;
    size_t _size;
    size_t _capacity;
};

template<class T, int DefaultCapacity = 10>
SeqList<T, DefaultCapacity>::SeqList(T* data = NULL, size_t size = 0)
:_data(new T[DefaultCapacity])
      , _size(size)
      , _capacity(DefaultCapacity)
{}

template<class T, int DefaultCapacity = 10>
SeqList<T, DefaultCapacity>::SeqList(const SeqList& s)
:_data(new T[DefaultCapacity])
, _size(s._size)
, _capacity(s._capacity)
{
    memcpy(_data, s._data, sizeof(T*)* _size);
}

template<class T, int DefaultCapacity = 10>
SeqList<T, DefaultCapacity>::~SeqList()
{
    if (_data)
    {
        delete[] _data;
    }
}

template<class T, int DefaultCapacity = 10>
void SeqList<T, DefaultCapacity>::CheckCapacity()
{
    if (_size >= _capacity)
    {
        _capacity *= 2;
        T* tmp = new T[_capacity];
        memcpy(tmp, _data, sizeof(T*)* _size);
        delete _data;
        _data = tmp;
    }
}

template<class T, int DefaultCapacity = 10>
void SeqList<T, DefaultCapacity>::PushBack(const T& x)
{
    CheckCapacity();
    _data[_size] = x;
    _size++;
}

template<class T, int DefaultCapacity = 10>
void SeqList<T, DefaultCapacity>::Print()
{
    for (int i = 0; i < _size; i++)
    {
        cout << _data[i] << "-";
    }
    cout << endl;
}

template<class T, int DefaultCapacity = 10>
void SeqList<T, DefaultCapacity>::PopBack()
{
    if (_size)
    {
        _size--;
    }
    else
    {
        cout << "seqlist is empty" << endl;
    }

}

template<class T, int DefaultCapacity = 10>
void SeqList<T, DefaultCapacity>::PushFront(const T& x)
{
    CheckCapacity();
    if (_size == 0)
    {
        _data[_size] = x;
    }
    else
    {
        for (int i = _size; i > 0; i--)
        {
            _data[i] = _data[i - 1];
        }
        _data[0] = x;
    }
    _size++;
}

template<class T, int DefaultCapacity = 10>
void SeqList<T, DefaultCapacity>::PopFront()
{
    if (_size)
    {
        for (int i = 0; i < _size - 1; i++)
        {
            _data[i] = _data[i + 1];
        }
        _size--;
    }
    else
    {
        cout << "Seqlist is empty" << endl;
    }
}

template<class T, int DefaultCapacity = 10>
void SeqList<T, DefaultCapacity>::Insert(size_t index, const T& x)
{
    assert(this);
    CheckCapacity();
    if (_size == 0)
    {
        cout << "Seqlist is empty" << endl;
    }
    else
    {
        if (index <= _size)
        {
            for (int i = _size; i > index; i--)
            {
                _data[i] = _data[i - 1];
            }
            _data[index] = x;
            _size++;
        }
        else
        {
            cout << "Index is wrong" << endl;
        }
    }
}

template<class T, int DefaultCapacity = 10>
void SeqList<T, DefaultCapacity>::Modified(size_t index, const T& x)
{
    assert(this);
    if (index < _size)
    {
        _data[index] = x;
    }
    else
    {
        cout << "Index is wrong" << endl;
    }
}

template<class T, int DefaultCapacity = 10>
int SeqList<T, DefaultCapacity>::Find(const T& x)
{
    assert(this);
    if (_size)
    {
        for (int i = 0; i < _size; i++)
        {
            if (_data[i] == x)
            {
                cout << "Find " << x << endl;
                return i;
            }
        }
        cout << "not find" << endl;
    }
    else
    {
        cout << "Seqlist is empty" << endl;
    }
    return 0;
}

template<class T, int DefaultCapacity = 10>
void SeqList<T, DefaultCapacity>::Remove(size_t index)
{
    assert(this);
    if (index < _size)
    {
        for (int i = index; i < _size - 1; i++)
        {
            _data[i] = _data[i + 1];
        }
        _size--;
    }
    else
    {
        cout << "Index is wrong or seqlist is empty" << endl;
    }
}



void Test1()
{
    SeqList<char> s1;
    s1.PushBack('1');
    s1.PushBack('2');
    s1.PushBack('3');
    s1.Print();
    s1.PopBack();
    /*s1.PopBack(); s1.PopBack(); s1.PopBack();*/
    s1.Print();
}

void Test2()
{
    SeqList<double> s1;
    s1.PushFront(1.1);
    s1.PushFront(2.2);
    s1.PushFront(3.3);
    s1.Print();
    /*s1.PopFront(); s1.PopFront(); s1.PopFront(); s1.PopFront();*/
    /*s1.Insert(3, 5);*/
    /*s1.Modified(2, 9);*/
    s1.Find(3.3);
    s1.Remove(3);
    s1.Print();
}
int main()
{
    Test1();
    //Test2();
    getchar();
    return 0;
}

你可能感兴趣的:(顺序表,模版,模版类)