C++模板实现顺序表+双向链表+栈+队列

Vector.h

#pragma once

//类名 Vector
//类型 Vector<T>
template<class T>
class Vector
{
public:
    Vector()      //构造函数函数名与类名相同
        :_start(NULL)
        , _finish(NULL)
        , _endofstorage(NULL)
    {}

    Vector<T>(Vector<T>& v)
    {
        if (v.Size() > 0)
        {
            _start = new T[v.Size()];      //只开辟原有数据所占空间大小,节省空间
            memcpy(_start, v._start, sizeof(T)*v.Size());
            if (_start)
            {
                _finish = _start + v.Size();
                _endofstorage = _start + v.Size();
            }
            else
            {
                _start = _finish = _endofstorage = NULL;
            }
        }
    }

    Vector<T>& operator=(Vector<T>& v)
    {
        if (this != &v)
        {
            ////传统写法
            //size_t size = v.Size();
            //T* tmp = new T[v.Size()];
            //memcpy(tmp, _start, sizeof(T)*size);
            //delete[] _start;
            //_start = tmp;
            //_finish = _start + size;
            //_endofstorage = _start + size;

            //现代写法
            swap(_start, v._start);
            swap(_finish, v._finish);
            swap(_endofstorage, v._endofstorage);
        }
        return *this;
    }

    ~Vector()
    {
        delete[] _start;
        _start = _finish = _endofstorage = NULL;
    }

    void PushBack(const T& x);
    void PopBack();
    void Insert(size_t pos, const T& x);
    void Erase(size_t pos);
    size_t Size();
    size_t Capacity();
    bool Empty();
    size_t Find(const T& x);
    void Print()
    {
        T* cur = _start;
        while (cur != _finish)
        {
            cout << *cur << " ";
            ++cur;
        }
        cout << endl;
    }

    const T& Back() const
    {
        return *(_finish - 1);
    }

    T& operator[] (size_t index)
    {
        return _start[index];
    }

protected:
    void Expand(size_t n);

protected:
    T* _start;
    T* _finish;
    T* _endofstorage;
};

//template<class T>
//Vector<T>::Vector()
//  :_start(NULL)
//  , _finish(NULL)
//  , _endofstorage(NULL)
//{}

template<class T>
void Vector<T>::Expand(size_t n)
{
    if (Empty())
    {
        _start = new T[3];
        _finish = _start;
        _endofstorage = _start + 3;
    }
    else if (n > Capacity())
    {
        size_t size = Size();
        T* tmp = new T[n];
        //memcpy(tmp, _start, sizeof(T)*size);
        for (size_t i = 0; i < size; ++i)
        {
            tmp[i] = _start[i];
        }

        delete[] _start;
        _start = tmp;
        _finish = _start + size;
        _endofstorage = _start + n;
    }
}

template<class T>
size_t Vector<T>::Size()
{
    return _finish - _start;
}

template<class T>
size_t Vector<T>::Capacity()
{
    return _endofstorage - _start;
}

template<class T>
bool Vector<T>::Empty()
{
    return _start == _finish;
}

template<class T>
void Vector<T>::PushBack(const T& x)
{
    Insert(Size(), x);
}

template<class T>
void Vector<T>::PopBack()
{
    Erase(Size() - 1);
}

template<class T>
void Vector<T>::Insert(size_t pos, const T& x)
{
    assert(pos <= Size());
    if (Size() >= Capacity())
    {
        Expand(Capacity() * 2);
    }
    T* end = _finish - 1;
    while (end >= _start + pos)
    {
        *(end + 1) = *end;
        --end;
    }
    _start[pos] = x;
    ++_finish;
}

template<class T>
void  Vector<T>::Erase(size_t pos)
{
    assert(pos < Size());
    T* cur = _start + pos + 1;
    while (cur != _finish)
    {
        *(cur - 1) = *cur;
        ++cur;
    }
    --_finish;
}

template<class T>
size_t Vector<T>::Find(const T& x)
{
    T* cur = _start;
    while (cur != _finish)
    {
        if (*cur == x)
        {
            return cur - _start;
        }
        ++cur;
    }
    return (size_t)-1;
}


void TestVector()
{
    Vector<int> v;
    v.PushBack(1);
    v.PushBack(2);
    v.PushBack(3);
    v.PushBack(4);
    v.Print();

    Vector<int> v1(v);
    v1.Insert(2, 5);
    v1.Print();

    Vector<int> v2;
    v2 = v1;
    v2.Insert(3, 6);
    v2.Print();
}

List.h

#pragma once

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

    ListNode(const T& x)
        :_data(x)
        , _next(NULL)
        , _prev(NULL)
    {}
};

template<class T>
class List
{
    typedef ListNode<T> Node;
public:
    List()
    {
        _head = new Node(T());
        _head->_next = _head;
        _head->_prev = _head;
    }

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

    List<T>& operator=(const Vector<T>& l)
    {
        if (this != &l)
        {
            swap(_head, l._head);
        }
        return *this;
    }

    ~List()
    {
        Clear();
        delete _head;
        _head = NULL;
    }

    void Clear()    //清理函数
    {
        Node* cur = _head->_next;
        while (cur != _head)
        {
            Node* next = cur->_next;
            delete cur;
            cur = next;
        }
        _head->_next = _head;
        _head->_prev = _head;
    }

    void PushBack(const T& x);
    void PopBack();
    void PushFront(const T& x);
    void PopFront();

    void Insert(Node* pos, const T& x);
    void Erase(Node* pos);
    Node* Find(const T& x)
    {
        Node* cur = _head->_next;
        while (cur != _head)
        {
            if (cur->_data == x)
            {
                return cur;
            }
            cur = cur->_next;
        }
        return NULL;
    }
    void Print() const;

    const T& Back() const
    {
        return _head->_prev->_data;
    }

    const T& Front() const
    {
        return _head->_next->_data;
    }

    size_t Size()
    {
        size_t size = 0;
        Node* cur = _head->_next; 
        while (cur != _head)
        {
            ++size;
            cur = cur->_next;
        }
        return size;
    }

    bool Empty() const
    {
        return _head->_next == _head;
    }

protected:
    Node* _head;
};

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

template<class T>
void List<T>::PushBack(const T& x)
{
    Insert(_head, x);
}

template<class T>
void List<T>::PopBack()
{
    Erase(_head->_prev);
}

template<class T>
void List<T>::PushFront(const T& x)
{
    Insert(_head->_next, x);
}

template<class T>
void List<T>::PopFront()
{
    Erase(_head->_next);
}

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

template<class T>
void List<T>::Erase(Node* pos)
{
    assert(pos && pos != _head);
    Node* prev = pos->_prev;
    Node* next = pos->_next;
    prev->_next = next;
    next->_prev = prev;
    delete pos;
}

void TestList()
{
    List<int> l;
    l.PushBack(1);
    l.PushBack(2);
    l.PushBack(3);
    l.PushBack(4);
    l.Print();
    ListNode<int>* cur = l.Find(4);
    l.Insert(cur, 5);
    l.Print();

    List<int> l1(l);
    l1.Print();
}

Stack.h

#pragma once

//适配器模式
//模板的模板参数

template<class T, class Container>
class Stack
{
public:
    void Push(const T& x)     //入栈
    {
        _con.PushBack(x);
    }

    void Pop()                //出栈
    {
        _con.PopBack();
    }

    const T& Top() const      //取栈顶元素
    {
        return _con.Back();
    }

    size_t Size()
    {
        _con.Size();
    }

    bool Empty()
    {
        return _con.Empty();
    }

protected:
    Container _con;
};

void TestStack()
{
    Stack<int, Vector<int>> s;
    s.Push(1);
    s.Push(2);
    s.Push(3);
    s.Push(4);
    while (!s.Empty())
    {
        cout << s.Top() << " ";
        s.Pop();
    }
    cout << endl;
}

Queue.h

#pragma once

template<class T, class Container>
class Queue
{
public:
    void Push(const T& x)       //入队列
    {
        _con.PushBack(x);
    }

    void Pop()                  //出队列
    {
        _con.PopFront();
    }

    const T& Front() const      //取队首元素
    {
        return _con.Front();
    }

    bool Empty() const
    {
        return _con.Empty();
    }
protected:
    Container _con;
};

void TestQueue()
{
    Queue<int, List<int>> q;
    q.Push(1);
    q.Push(2);
    q.Push(3);
    q.Push(4);

    while (!q.Empty())
    {
        cout << q.Front() << " ";
        q.Pop();
    }
    cout << endl;
}

test.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <iostream>
using namespace std;

#include "Vector.h"
#include "List.h"
#include "stack.h"
#include "Queue.h"

int main()
{
    TestVector();
    TestList();
    TestStack();
    TestQueue();
    system("pause");
    return 0;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/adorable_/article/details/80351730
文章标签: C++ 模板 适配器
个人分类: C++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭