c++实现动态顺序表

seqlist.h

#pragma once
#define _SEQ_LIST_
#ifdef _SEQ_LIST_
#include<iostream>
#include<assert.h>
#include<string>
using namespace std;

#define DEFAULT_CAPACITY 3
typedef int DataType;

typedef struct FindRet
{
    bool isFind;    // 是否找到的标示
    size_t index;   // 找到数据的下标
}Findret;

class SeqList
{
public:
    SeqList(DataType *arr = NULL, size_t size = 0, size_t capacity = DEFAULT_CAPACITY)

    {
        _arr = new DataType[DEFAULT_CAPACITY];
        _size = size;
        _capacity = capacity;
    }
    void Swap(SeqList &s)
    {
        /*DataType *tmp = s._arr; s._arr = _arr; _arr = tmp;*/
        swap(_arr, s._arr);  //swap是c++库函数
        swap(_size, s._size);
        swap(_capacity, s._capacity);
    }
    SeqList(const SeqList &s) :_arr(NULL)
    {
       SeqList tmp(s._arr);
        Swap(tmp);

    }
    SeqList& operator= (SeqList sList)
    {
        Swap(sList);
        return *this;
    }
    ~SeqList()
    {
        if (_arr)
        {
            delete[] _arr;
        }
    }
private:
    void _Cheekcapacity()
    {
        if (_size == _capacity) //判断链表是否已满
        {
            _capacity = _capacity * 2;
            DataType* tmp = new DataType[_capacity];
            memcpy(tmp, _arr, sizeof(DataType)* _size);
            delete[] _arr;
            _arr = tmp;
        }
    }
public:
    void PushBack(const DataType& x) //尾插
    {
        assert(this);
        _Cheekcapacity();
        _arr[_size] = x;
        _size++;
    }
    void PopBack() //尾删
    {
        assert(this);
        if (_size == 0)
        {
            cout << "SeqList is empty" << endl;
            return;
        }
        _arr[ --_size] = 0;
    }
    void PushFront(const DataType& x) //头插
    {
        assert(this);
        _Cheekcapacity();
        for (int i = _size; i > 0; i--)
        {
            _arr[i] = _arr[i - 1];
        }
        _arr[0] = x;
        _size++;
    }
    void PopFront() //头删
    {
        assert(this);
        if (_size == 0)
        {
            cout << "SeqList is empty" << endl;
            return;
        }
        for (size_t i = 0; i < _size - 1; i++)
        {
            _arr[i] = _arr[i + 1];
        }
        _size--;
    }
    void Print()
    {
        assert(this);
        if (_size == 0)
        {
            cout << "SeqList is empty" << endl;
            return;
        }
        for (size_t i = 0; i < _size; i++)
        {
            cout << _arr[i] << "->";
        }
        cout <<"NULL"<<endl;
    }
    void Insert(size_t index, const DataType& x)
    {
        assert(this);
        _Cheekcapacity();
        if (index < _size)
        {
            for (size_t i = _size; i > index; i--)
            {
                _arr[i] = _arr[i - 1];
            }
            _arr[index] = x;
            _size++;
        }

    }
    void Modified(size_t index, const DataType& x) //将index位置的元素修改为x
    {
        assert(this);
        if (index < _size)
        {
            _arr[index] = x;
        }
    }
    void Remove(size_t index)
    {
        assert(this);
        if (index < _size)
        {
            for (size_t i = index; i < _size - 1; i++)
            {
                _arr[i] = _arr[i + 1];
            }
            _size--;
        }
    }
    FindRet Find(const DataType& x)
    {
        Findret ret;
        assert(this);
        for (size_t i = 0; i < _size; i++)
        {
            if (_arr[i] == x)
            {
                ret.isFind = true;
                ret.index = i;
                cout << "find " << x << endl;
                return ret;
            }
        }
        ret.isFind = false;
        cout << "not find "<< x << endl;
        return ret;
    }
private:
    DataType *_arr;
    size_t _size;
    size_t _capacity;
};

#endif //_SEQ_LIST

test.cpp

#include"seqlist.h"

void Test()
{
    SeqList s1;
    s1.PushBack(1);
    s1.PushBack(2);
    s1.PushBack(3);
    s1.PushBack(4);
    //s1.PopBack();
    //s1.PopBack();
    s1.Insert(3, 0);
    s1.Print();
    s1.Modified(0, 0);
    s1.Print();
    s1.Remove(3);
    s1.Find(4);
    s1.Find(1);
    s1.Print();
}
void Test2()
{
    SeqList s1;
    s1.PushFront(1);
    s1.PushFront(2);
    s1.PushFront(3);
    s1.PushFront(4);
    s1.PopFront();
    s1.PopFront();
    s1.Print();
}

int main()
{
    Test();
    getchar();
    return 0;
}

你可能感兴趣的:(C++,顺序表)