(C++模板类)顺序表、双向链表

来源:互联网 发布:淘宝京东商城 编辑:程序博客网 时间:2024/06/07 20:28

顺序表

#include<iostream>using namespace std;#include<string>#include<assert.h>#define _CRT_SECURE_NO_WARNINGS#pragma warning( disable : 4996)template<typename T>class SeqList{public:    SeqList()        :_data(NULL)        , _size(0)        , _capacity(0)    {}    /*SeqList(const T& s)    :_data(s._data)    , _size(s._size)    , _capacity(s._capacity)    {}    SeqList& operator=(SeqList& s)    {    }*/    T& operator[](size_t pos)    {        assert(pos < _size);        return _data[pos];    }    ~SeqList()    {        if (_data)        {            delete[] _data;            _size = _capacity = 0;            _data = NULL;        }    }    void CheckCapacity()    {        size_t NewSize = _size ? _size * 2 : 3;        T* tmp = new T[NewSize];        for (size_t i = 0; i < _size; ++i)        {            tmp[i] = _data[i];        }        delete[] _data;        _data = tmp;        _capacity = NewSize;    }    void PushBack(const T& x)    {        CheckCapacity();        _data[_size] = x;        ++_size;    }    void PopBack()    {        assert(_size);        --_size;    }    void Insert(size_t pos, const T& x)    {        assert(pos);        assert(_size);        CheckCapacity();        for (size_t end = pos; end > pos; ++end)        {            _data[end] = _data[end + 1];        }        _data[pos] = x;        _size++;    }    void Erase(size_t pos)    {        assert(pos);        for (size_t i = pos; i < _size; ++i)        {            _data[i - 1] = _data[i];        }        --_size;    }    void Print()    {        for (size_t i = 0; i < _size; ++i)        {            cout << _data[i]<<" ";        }        cout << endl;    }private:    T* _data;    int _size;    int _capacity;};

双向链表

#include<iostream>using namespace std;#include<string>#include<assert.h>#define _CRT_SECURE_NO_WARNINGS#pragma warning( disable : 4996)template<typename T>struct ListNode{    T _data;    ListNode* _prev;    ListNode* _next;    ListNode(T x)        :_data(x)        , _prev(NULL)        , _next(NULL)    {}};template<typename T>class List{    typedef ListNode<T> Node;public:    List()    {        _head = new Node(T());//匿名对象        _head->_next = _head;        _head->_prev = _head;    }    ~List()    {        Node* cur = _head;        while (cur->_next!=_head)        {            Node* next = cur->_next;            delete cur;            cur = next;        }        _head->_next = _head;        _head->_prev = _head;    }    void PushBack(const T& x)    {        Insert(_head, x);    }    void PopBack()    {        Erase(_head->_prev);    }    void PushFront(const T& x)    {        Insert(_head->_next, x);    }    void PopFront()    {        Erase(_head->_next);    }    void Insert(Node* pos, const T& x)    {        assert(pos);        Node* prev = pos->_prev;        Node* tmp = new Node(x);        prev->_next = tmp;        tmp->_next = pos;        pos->_prev = tmp;        tmp->_prev = prev;    }    void Erase(Node* pos)    {        Node* prev = pos->_prev;        Node* next = pos->_next;        prev->_next = next;        next->_prev = prev;        delete pos;    }    void Print()    {        Node* tmp = _head->_next;        while (tmp != _head)        {            cout << tmp->_data<<"->";            tmp = tmp->_next;        }        cout << endl;    }private:    Node* _head;};