C++_模板实现顺序表和链表

顺序表

#pragma once
#include <iostream>
#include <string>
#include <string.h>
using namespace std;

// 模板实现顺序表--考虑深层次的深浅拷贝问题 
template<class T>
class SeqList
{
public:
    SeqList();
    SeqList(const SeqList& s);
    SeqList& operator=(SeqList& s);
    ~SeqList();

    void PushBack(const T& x);
    void PopBack();
    void Insert(size_t pos, const T& x);
    void Erase(size_t pos);
    size_t size()
    {
        return _size;
    }
    T& operator[](size_t pos);
    void Print();
private:
    T* _a;
    size_t _size;
    size_t _capacity;
};

template<class T>
SeqList<T>::SeqList()
:_a(new T[10])
, _size(0)
, _capacity(10)
{
}

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

template<>         //特化string,本来应该特化所有内置类型,其他自定义类型用for循环调用自定义类型的operate=重载
SeqList<string>::SeqList(const SeqList& s)
:_a(new string[s._capacity])
, _size(s._size)
, _capacity(s._capacity)
{
    for (size_t i = 0; i < _size; i++)
    {
        _a[i] = s._a[i];
    }
}

template<class T>
SeqList<T>& SeqList<T>::operator=(SeqList& s)
{
    if (_a != s._a)
    {
        delete[] _a;
        _a = new T[s._capacity];
        _size = s._size;
        _capacity = s._capacity;
        memcpy(_a, s._a, _size*sizeof(T));
    }
    return *this;
}

template<>
SeqList<string>& SeqList<string>::operator=(SeqList& s)
{
    if (_a != s._a)
    {
        delete[] _a;
        _a = new string[s._capacity];
        _size = s._size;
        _capacity = s._capacity;
        for (size_t i = 0; i < _size; i++)
        {
            _a[i] = s._a[i];
        }
    }
    return *this;
}

template<class T>
SeqList<T>::~SeqList()
{
    delete[] _a;
    _a = NULL;
    _size = 0;
    _capacity = 0;
}

template<class T>
void SeqList<T>::PushBack(const T& x)
{
    if (_size == _capacity)
    {
        //扩容
    }
    _a[_size] = x;
    _size++;

}

template<class T>
void SeqList<T>::PopBack()
{
    if (_size > 0)
    {
        _size--;
    }
}

template<class T>
void SeqList<T>::Insert(size_t pos, const T& x)
{
    if (_size == _capacity)
    {
        //扩容
    }
    for (int i = _size; i > pos;  i--)
    {
        _a[i] = _a[i - 1];
    }
    _size++;
}

template<class T>
void SeqList<T>::Erase(size_t pos)
{
    delete[] _a;
    _a = NULL;
    _size = 0;
    _capacity = 0;
}

template<class T>
T& SeqList<T>::operator[](size_t pos)
{
    if (pos >= 0 && pos < _size)
    {
        return _a[pos];
    }
}

template<class T>
void SeqList<T>::Print()
{
    for (size_t i = 0; i < _size; i++)
    {
        cout << _a[i] << endl;
    }
}

链表

#pragma once
#include <iostream>
#include <string>
#include <string.h>
using namespace std;

// 带头节点的双向循环链表--思考结构的优势 
template<class T>
struct Node
{
    T _data;
    Node* _next;
    Node* _prev;
};

template<class T>
class List
{
    //typedef ListNode Node;
public:
    List();
    List(const List& l);
    List<T>& operator=(const List& l);
    ~List();

    void PushBack(const T& x);
    void PopBack();
    void PushFront(const T& x);
    void PopFront();
    void Insert(Node<T>* pos, const T& x);
    void Erase(Node<T>* pos);
    void Print();
    T& Front()
    {
        return _head->_next->_data;
    }
private:
    Node<T>* _head;
};

template<class T>
List<T>::List()
:_head(new Node<T>)
{
    _head->_next = _head;
    _head->_prev = _head;
    _head->_data = 0;
}

template<class T>
List<T>::List(const List& l)
:_head(new Node<T>)
{
    _head->_next = _head;
    _head->_prev = _head;
    _head->_data = 0;
    Node<T> *cur = l._head->_next;
    while (cur != l._head)
    {
        PushBack(cur->_data);
        cur = cur->_next;
    }
}

template<class T>
List<T>& List<T>::operator=(const List& l)
{
    if (_head != l._head)
    {
        while (_head->_next != _head)
        {
            PopBack();
        }
        Node<T> *cur = l._head->_next;
        while (cur != l._head)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }
    return *this;
}

template<class T>
List<T>::~List()
{
    while (_head->_next != _head)
    {
        PopBack();
    }
    delete _head;
}


template<class T>
void List<T>::PushBack(const T& x)
{
    Node<T> *cur = new Node<T>;
    cur->_data = x;
    Node<T> *tmp = _head->_prev;
    tmp->_next = cur;
    cur->_prev = tmp;
    cur->_next = _head;
    _head->_prev = cur;
}

template<class T>
void List<T>::PopBack()
{
    Node<T> *cur = _head->_prev;
    cur->_prev->_next = _head;
    _head->_prev = cur->_prev;
    delete cur;

}

template<class T>
void List<T>::PushFront(const T& x)
{
    Node<T> *cur = new Node<T>;
    cur->_data = x;
    Node<T> *tmp = _head->_next;
    _head->_next = cur;
    cur->_prev = _head;
    cur->_next = tmp;
    tmp->_prev = cur;
}
template<class T>
void List<T>::PopFront()
{
    Node<T> *cur = _head->_next;
    _head->_next = cur->_next;
    cur->_next->_prev = _head;
    delete cur;
}

template<class T>
void List<T>::Insert(Node<T>* pos, const T& x)
{
    Node<T> *cur = new Node<T>;
    cur->_data = x;
    Node<T> *tmp = pos->_prev;
    tmp->_next = cur;
    cur->_prev = tmp;
    cur->_next = pos;
    pos->_prev = cur;
}

template<class T>
void List<T>::Erase(Node<T>* pos)
{
    Node<T> *cur = pos->_prev;
    cur->next = pos->_next;
    pos->_next->_prev = cur;
    delete pos;
}

template<class T>
void List<T>::Print()
{
    Node<T> *cur = _head->_next;
    while (cur != _head)
    {
        cout << cur->_data << endl;
        cur = cur->_next;
    }
}

栈和队列

// 1.适配器模式 
// 2.模板的模板参数 实现栈
template<class T, template<class T>class Container = SeqList>
class Stack
{
public:
    void Push(const T& x)
    {
        _con.PushBack(x);
    }

    const T& Top()
    {
        return _con[_con.size()-1];
    }

    void Pop()
    {
        _con.PopBack();
    }
protected:
    Container<T> _con;
};

//模板参数  实现队列
template<class T,class Container = List<T>>
class Queue
{
public:
    void Push(const T& x)
    {
        _con.PushBack(x);
    }
    T& Front()
    {
        return _con.Front();
    }
    void Pop()
    {
        _con.PopFront();
    }
protected:
    Container _con;
};

猜你喜欢

转载自blog.csdn.net/cute_shuai/article/details/80399428
今日推荐