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

顺序表

#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;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值