C++:Vector和List的实现

Vector的实现

//test.h
#pragma once

#include <iostream>
#include <cstdio>
#include <string.h>
#include <assert.h>

using namespace std;

typedef int DataType;

#define TESTHEADER printf("\n================%s===============\n", __FUNCTION__)

class Vector
{
public:
    Vector();
    Vector(const Vector& v);
    ~Vector();
    Vector& operator = (Vector& v);
    void Swap(Vector& v);
    size_t Size()const;
    size_t Capacity()const;
    void Reserve(size_t n);
    void Resize(size_t n, DataType);
    DataType& operator[](size_t pos);
    void PushBack(DataType v);
    void PopBack();
private:
    void Expand(size_t n);
    DataType* _start;
    DataType* _finish;
    DataType* _endofstorage;
};

//test.cc
/*
 *
 *构造函数
 *
 */
#include "test.h"

Vector::Vector()
    :_start(NULL)
     ,_finish(NULL)
     ,_endofstorage(NULL)
{
}

/*
 *
 * 拷贝构造函数
 *
 */
//v(v1)
Vector::Vector(const Vector& v)
{
    _start = new DataType[v.Size()];
    memcpy(_start, v._start, v.Size() * sizeof(DataType));
    _finish = _start + v.Size();
    _endofstorage = _start + v.Size();
}

/*
 *
 * 扩展空间
 *
 */
void Vector::Expand(size_t n)
{
    if(n > Capacity())
    {
        size_t size = Size();
        DataType* tmp = new DataType [n];
        if(_start != NULL)
        {
            memcpy(tmp, _start, size * sizeof(DataType));
            delete [] _start;
        }
        _start = tmp;
        _finish = _start + size;
        _endofstorage = _start + n;
    }
}


/*
 *
 * 预先分配空间并且初始化
 *
 */
void Vector::Resize(size_t n, DataType value = DataType())
{
    if(n <= Size())
    {
        _finish = _start + n;
        _endofstorage = _start + n;
    }
    else
    {
        if(n > Capacity())
        {
            Expand(n);
            size_t i = 0;
            for(; i < Size(); ++i)
            {
                _start[i] = value;
            }
        }//end if(n > Capacity())
    }//end else
}


/*
 *
 * 预先分配空间但是不进行初始化
 *
 */
void Vector::Reserve(size_t n)
{
    Expand(n);
}

/*
 *
 * 尾插一个元素
 *
 */

void Vector::PushBack(DataType v)
{
    if(_finish == _endofstorage)
    {
        size_t new_capacity = Capacity() == 0 ? 3 : (Capacity() * 2);
        Expand(new_capacity);
    }
    *_finish = v;
    ++_finish;
}

/*
 *
 * 交换内置类型(成员变量)
 *
 */
void Vector::Swap(Vector& v)
{
    swap(_start, v._start);
    swap(_finish, v._finish);
    swap(_endofstorage, v._endofstorage);
}


/*
 *
 * 赋值
 *
 */
//v1 = v2
//operator = (v1, v2)
Vector& Vector::operator = (Vector& v)
{
    Swap(v);
    return *this;
}

//v[pos]
DataType& Vector::operator[](size_t pos)
{
    assert(pos < Size());
    return _start[pos];
}


/*
 *
 * 析构函数
 *
 */
Vector::~Vector()
{
    if(_start)
    {
        delete [] _start;
        _start = _finish = _endofstorage = NULL;
    }
}

/*
 *
 * 返回Vector的有效字符个数
 *
 */
size_t Vector:: Size()const
{
    return _finish - _start;
}

/*
 *
 * Vector的容量
 *
 */
size_t Vector::Capacity()const
{
    return _endofstorage - _start;
}

/*
 *
 * 尾删一个元素
 *
 */
void Vector::PopBack()
{
    --_finish;
}

List的实现

//list.h
#pragma once
#include <iostream>
#include <cstdio>
#include <assert.h>
using namespace std;

typedef int DataType;
struct ListNode
{
    DataType _data;
    ListNode* _next;
    ListNode* _prev;
    ListNode(DataType x)
        :_data(x)
         ,_next(NULL)
         ,_prev(NULL)
    {

    }
};

class List
{
    typedef ListNode Node;
public:
    List();//构造函数
    ~List();//析构函数
    List(const List& l);//拷贝构造
    List& operator = (List l);//赋值构造函数
    void PushBack(DataType l);//尾插函数
    void PushFront(DataType l);//头插函数
    void PopBack();//尾删函数
    void PopFront();//头删函数
    void Insert(Node* pos, DataType l);//往pos处插入一个结点
    void Erase(Node* pos);//删除某个位置上的某个结点
    void Print();
    void Clean();//清空
private:
    Node* _head;
};

//test.cc
#include "list.h"

/*
 *
 * 构造函数
 *
 */
List::List()
{
    _head = new Node(DataType());
    _head -> _next = _head;
    _head -> _prev = _head;
}

void List:: Insert(Node* pos, DataType l)
{
    assert(pos);
    Node* prev= pos -> _prev;
    Node* next = pos;
    Node* new_node = new Node(l);

    //new_node VS next;
    new_node -> _next = next;
    next -> _prev = new_node;

    //new_node VS prev
    prev -> _next = new_node;
    new_node -> _prev = prev;
}

void List::Erase(Node* pos)
{
    Node* next = pos;
    Node* prev = pos -> _prev;
    prev -> _next = next;
    next -> _prev = prev;
}


void List::PushBack(DataType l)
{
    this -> Insert(_head, l);
}

//l1(l2)
List::List(const List& l)
{
    _head = new Node(DataType());
    _head -> _prev = _head;
    _head -> _next = _head;

    Node* cur = l._head -> _next;
    while(cur != l._head)
    {
        this -> PushBack(cur -> _data);
        cur = cur -> _next;
    }
}

void List::PushFront(DataType l)
{
    Insert(_head -> _next, l);
}

/*
 *
 * 清空
 *
 */
void List::Clean()
{
    Node* cur = _head -> _next;
    while(cur != _head)
    {
        Node* next = cur -> _next;
        delete cur;
        cur = next;
    }
}

void List::Print()
{
    assert(_head -> _next != _head);
    Node* cur = _head -> _next;
    while(cur != _head)
    {
        cout << cur -> _data << " ";
        cur = cur -> _next;
    }
    cout << endl;
}
List::~List()
{
    Clean();
    delete _head;
}

void List::PopBack()
{
    Erase(_head -> _prev);
}

//l1 = l2
List& List::operator = (List l)
{
    swap(this -> _head, l._head);
    return *this;
}

void List::PopFront()
{
    Erase(_head -> _next);
}

猜你喜欢

转载自blog.csdn.net/qq_41027326/article/details/81064532