模板实现顺序表和双向链表

模板详解可以点这里

  • 1、实现动态顺序表
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
//区别下面两种概念
//类的类型: Vector<T>
//类: Vector
template<class T>
class Vector
{
protected:

    T* _first;//数组的首地址
    T* _finish;//计数作用,相当于Size
    T* _capacity;//容量

public:
    Vector()//构造函数
        :_first(NULL)
        ,_finish(NULL)
        ,_capacity(NULL)
    {
        ;
    }
    ~Vector()//析构函数
    {
        delete[] _first;
        _first = _finish = _capacity = NULL;
    }
    //v2(v1)
    Vector(Vector<T>& v)//拷贝构造函数
    {
        if (this != &v)
        {
        //传统写法
            T* tmp = new T[v.Capacity()];//创建临时变量
            size_t i = 0;
            for (i = 0; i < v.Size(); i++)//深拷贝,如果顺序表中插入字符串,浅拷贝就会出现问题
            {
                tmp[i] = v._first[i];
            }
            delete[] _first;
            _first = tmp;
            _finish = _first + v.Size();
            _capacity = _first + v.Capacity();

        }
    }
    //v2 = v1
    Vector<T>& operator = ( Vector<T> &v)//拷贝赋值函数
    {
        if (this != &v)
        {
        //现代写法
            Vector<T>tmp(v);//创建临时对象
            Swap(tmp);//使用Swap函数交换
        }
        return *this;
    }

    void Swap(Vector<T> &s)
    {
        swap(_first,s._first);
        swap(_finish,s._finish);
        swap(_capacity,s._capacity);
    }
    void PushBack(const T& x)
    {
        Insert(Size(), x);
    }
    void PopBack()
    {
        Erase(Size() - 1);
    }
    void PushFront(const T& x)
    {
        Insert(*_first, x);
    }
    void PopFront()
    {
        Erase(0);
    }
    void Expand(size_t n)//扩容函数
    {
        if (Empty())//第一次插入时,给数组先开辟4个大小的空间
        {
            _first = _finish = new T[4];
            _capacity = _first + 4;
        }
        else
        {
            T* tmp = new T[n];
            size_t size = Size();
            memset(tmp, 0X00, sizeof(tmp)*n);
            for (size_t i = 0; i < size; i++)
            {
                tmp[i] = _first[i];
            }
            delete[] _first;

            _first = tmp;
            _finish = _first + size;
            _capacity = _first + n;

        }
    }
    void Insert(size_t pos,const T&x)
    {
        assert(pos <= Size());
        if (Size() >= Capacity())//判断容量是否需要扩容
        {
            Expand(2 * Capacity());
        }
        T* end = _finish - 1;

        while (end > _first + pos - 1)//从插入的位置元素向后移动
        {
            *(end + 1) = *end;
            --end;
        }
        *(_first + pos) = x;
        ++_finish;
    }

    void Erase(size_t pos)
    {
        assert(pos < Size() && pos >= 0);

        T* pos_next = _first + pos;
        while (pos_next < _finish)//从删除pos的下一个位置向前赋值
        {
            *(pos_next) = *(pos_next+1);
            pos_next++;
        }
        --_finish;
    }
    size_t Find(const T& x)
    {
        T* start = _first;
        int i = 0;
        for (i = 0; i < _finish; i++)
        {
            if (x == *start)
            {
                return i;
            }
        }
        return -1;
    }
    T& operator[](size_t pos)//修改某个位置
    {
        assert(pos < Size() && pos >= 0);
        return _first[pos];
    }
    const T& operator[](size_t pos) const
    {
        assert(pos < Size() && pos >= 0);
        return _first[pos];
    }
    size_t Size()
    {
        return _finish - _first ;
    }
    size_t Capacity()
    {
        return _capacity - _first;
    }
    bool Empty()
    {
        if (!(_finish - _first))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    void show()
    {
        T* cur = _first;
        size_t i = 0;
        for (i = 0; i < Size(); i++)
        {
            cout << *(cur+i) << " ";
        }
        cout << endl;
    }
};
  • 2、实现双向带头链表
#include <iostream>

using namespace std;

template<class T>
 struct ListNode
{
     ListNode<T>*_next;
     ListNode<T>* _prev;
     T _data;

};

template<class T>
class List
{
protected:
    Node* _head;

public:
    typedef ListNode<T> Node;

    List()//构造函数,初始化头结点
    :_head(new Node)
    {
        _head->_next = _head;
        _head->_prev = _head;
    }
    ~List()//析构函数
    {
        Clean();
        delete _head;
        _head = NULL;
    }
    void Clean()//释放插入的节点
    {
        Node* cur = _head->_next;

        while (_head != cur)
        {
            Node* tmp = cur;//保存当前节点
            cur = cur->_next;//指向下一个
            delete tmp;//释放保存的当前节点
        }
        _head->_next = _head;
        _head->_prev = _head;
    }
    //L1(L2)
    List(const List<T>& L)//拷贝构造函数
        :_head(new Node)
    {
        _head->_next = _head;
        _head->_prev = _head;

        Node* cur = L._head->_next;
        while (L._head != cur)
        {
            this->PushBack(cur->_data);//使用头插法给this指针的头结点插入节点
            cur = cur->_next;
        }
    }
    List<T>& operator = (const List<T>& L)//拷贝赋值函数
    {
        List<T> tmp(L);
        if (this != &L)
        {
            swap(_head,tmp._head);
        }
        return *this;
    }
    void PushBack(T x)
    {
        //Insert(_head->_prev, x);
        Node* tail = _head->_prev;
        Node* NewNode = new Node;
        NewNode->_data = x;

        tail->_next = NewNode;
        NewNode->_prev = tail;

        _head->_prev = NewNode;
        NewNode->_next = _head;

    }
    void PopBack()
    {
        Erase(_head->_prev);
    }
    void PushFront(T x)
    {
        Insert(_head->_next, x);
    }
    void PopFront()
    {
        Erase(_head->_next);
    }
    Node* Find(T x)
    {
        Node*cur = _head->_next;
        while (_head != cur)
        {
            if (x == cur->_data)
            {
                return cur;
            }
            cur = cur->_next;
        }
        return NULL;
    }
    void Insert(Node* pos, T x)
    {
    //创建一个新节点,将新节点插入pos节点和pos的前一个节点,三个节点链接好了
        Node* NewNode = new Node;
        NewNode->_data = x;
        Node* PosPrev = pos->_prev;

        NewNode->_next = pos;
        pos->_prev = NewNode;

        PosPrev->_next = NewNode;
        NewNode->_prev = PosPrev;

    }
    void Erase(Node* pos)
    {
    //将pos的前一个节点和后一个节点链接起来,再释放掉pos位置节点
        Node* PosPrev = pos->_prev;
        Node* PosNext = pos->_next;

        delete pos;
        pos = NULL;
        PosPrev->_next = PosNext;
        PosNext->_prev = PosPrev;

    }
    void Show()
    {
        Node* cur = _head->_next;
        while (cur != _head)
        {
            cout << cur->_data<<" ";
            cur = cur->_next;
        }
        cout << endl;
    }
    bool Empty()
    {
        if (_head->_next == _head)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值