c++---實現動態線性表

c++實現動態線性表

   之前在學習c語言的時候用c語言實現了動態線性表。現在再使用c++實現一下動態線性表。
   相關數據結構方面就不多說了。在之前的博客裏也有。下面就直接來實現吧。

vector
   這裏使用指針來遍歷數組,這樣在算size,capacity的時候,直接用指針相減的方式就可以得到元素個數,以及容量。

Vector.h

#include <iostream>
#include<assert.h>
#include<stdio.h>
#include<string.h>
//用typedef定義int爲存儲類型,想更改直接更改這一句即可。
typedef int DataType;

class Vector
{
public:
    //構造函數。
    Vector()
    {
        _first = new DataType[3];
        _finish = _first;
        _endofstorage = _first + 3;
    }
    //拷貝構造
    Vector(const Vector& v)
    {
        _first = new DataType[v.Size()];
        memmove(_first, v._first, v.Size()*sizeof(DataType));
        _finish = _first + v.Size() ;
        _endofstorage = _finish ;
    }
    //賦值運算符的重載
    Vector& operator=(Vector v);
    //析構函數
    ~Vector()
    {
        delete[] _first;
    }
    //順序表的有效長度
    size_t Size() const
    {
        return _finish - _first ;
    }
    //順序表的容量
    size_t Capacity() const
    {
        return _endofstorage - _first ;
    }
    //擴容順序表
    void Expand(size_t n);
    //尾插
    void PushBack(DataType x);
    //截取容量
    void Reserve(size_t n);
    //尾刪
    void PopBack();
    //任意位置插入
    void Insert(size_t pos, DataType x);
    //任意位置刪除
    void Erase(size_t pos);
    //查找元素
    size_t Find(DataType x);
    //打印當前順序表
    void Print();
private:
    //指向第一個元素的指針
    DataType* _first;
    //指向最後一個有效元素的下一個位置
    DataType* _finish;
    //順序表容量的下一個位置
    DataType* _endofstorage;
};

Vector.cpp

#include"Vector_List1.h"

    //賦值運算符的重載可以使用傳值的方式進行
    //在傳值的時候默認調用了拷貝構造函數,進行了深拷貝
    //而當前這個傳入的v就是我們想要的賦值之後的結果
    //將當前的順序表與順序表v一交換,就可以不用再自己實現深拷貝
    Vector& Vector::operator=(Vector v)
    {
        size_t size = v.Size();
        DataType *tmp = v._first;
        v._first = _first;
        _first = tmp;
        _finish = _first + size;
        _endofstorage = _finish;
        return *this;
    }
    void Vector::Expand(size_t n)
    {
        DataType *tmp = new DataType[n];
        size_t size = Size();
        memmove(tmp, _first, Size()*sizeof(DataType));
        delete[] _first;
        _first = tmp;
        _finish = _first + size;
        _endofstorage = _first + n;
    }
    void Vector::PushBack(DataType x)
    {
        if (_finish > _endofstorage)
            Expand(2 * Capacity());
        *_finish = x;
        _finish++;
    }
    void Vector::PopBack()
    {
        assert(_first < _finish);
        _finish--;
    }
    void Vector::Insert(size_t pos, DataType x)
    {
        assert(pos<Size());
        if(_finish >= _endofstorage)
            Expand(2*Capacity());
        memmove(_first+pos+1,_first+pos,Size()-pos+1);
        *(_first+pos) = x;
    }
    void Vector::Erase(size_t pos)
    {
        assert(pos<Size());
        memmove(_first+pos,_first+pos+1,(Size()-pos-1)*sizeof(DataType));
        _finish--;
    }
    size_t Vector::Find(DataType x)
    {
        DataType *tmp = _first;
        while(tmp != _finish)
        {
            if(*tmp == x)
                return tmp-_first;
            else
                tmp++;
        }
        return -1;
    }
    //截取n個字符
    void Vector::Reserve(size_t n)
    {
        //如果n<capacity,則什麼都不做,將其容量降爲size與n之間的最大值
        //會改變capacity,不會改變size,若n>capacity擴容,
        if(n<Capacity())
        {
            _endofstorage = _first + ( n > Size() ? n : Size());
            return;
        }
        else if(n>Capacity())
        {
            Expand(n);
            return;
        }
        else
            return;
    }      
    void Vector::Print()
    {
        DataType *tmp = _first;
        while (tmp != _finish)
        {
            printf("%d ", *tmp);
            tmp++;
        }
        printf("\n");
    }
int main()
{
    Vector v;
    Vector v1(v);
    v.PushBack(1);
    v.PushBack(2);
    v.PushBack(3);
    v.PushBack(4);
    v.PushBack(5);
    v.PushBack(6);
    v.Print();
    v1 = v;
    v1.Print();
    v1.Erase(2);
    v1.Print();
    size_t ret = v1.Find(3);
    printf("%lu\n",ret);
    ret = v1.Find(2);
    printf("%lu\n",ret);
    ret = v1.Find(5);
    printf("%lu\n",ret);
    v1.Reserve(3);
    v1.Print();
    return 0;
}
發佈了65 篇原創文章 · 獲贊 31 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章