C++实现动态顺序表


#define _CRT_SECURE_NO_WARNINGS 1

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

typedef int DataType;

class SeqList
{
public:
           SeqList()
                    :_array(NULL )
                    , _size(0)
                    , _capicity(0)
           {}
           SeqList(const SeqList & sList)
                    :_array(new DataType [sList ._size])
                    , _size( sList._size)
                    , _capicity( sList._capicity)
           {
                    //memcpy(_array, sList._array, sizeof(sList._array));
                    memcpy(_array, sList._array, sizeof( DataType)*_size);
           }
           SeqList& operator=(const SeqList& sList)
           {
                    if (&sList != this)
                    {
                              DataType *tmp = new DataType [sList ._size];
                              delete[] _array;

                              _array = tmp;
                              _size=sList ._size;
                              _capicity = sList._capicity;
                              memcpy(_array, sList._array, sizeof( DataType)*_size);
                    }
                    return *this ;
           }

           void CheckCapacity();              //测容
           void PushBack(const DataType & x); //尾插
           void PopBack();                      //尾删
           void PushFront(const DataType & x); //头插
           void PopFront();             //头删
           int Find(const DataType & x);  //查找数据
           void Print();          //打印顺序表
           void Insert(size_t pos, const DataType& x);  //插入数据
           void Erase(size_t pos);      //删除某位置的数据
           int Remove(const DataType & x);    //删除x

           ~SeqList()
           {
                    if (_array != NULL )
                    {
                              delete[] _array;
                              _size = 0;
                              _capicity = 0;
                    }
           }

private:
           DataType* _array;   //数据块指针
           size_t  _size;        //定义当前有效数据的个数
           size_t _capicity;    //容量
 
};

//测容,如果容量不够要进行扩容
void SeqList::CheckCapacity()
{
           if (_size >= _capicity)
           {
                    _capicity = 2 * _capicity + 5;
                    _array = (DataType *)realloc(_array, _capicity*sizeof (DataType ));
           }
}

//打印顺序表
void SeqList::Print() 
{
           for (int i = 0; i < _size; ++i)
           {
                    cout << _array[i] << " " ;
           }
           cout << endl;
}

//在尾部添加数据
void SeqList::PushBack(const DataType & x)
{
           CheckCapacity();    //添加数据前要进行测容
           _array[_size++] = x ;     //这里注意:添加完数据意思要给size加1
}

//尾删
void SeqList::PopBack()
{
           if (_size == 0)
           {
                    cout << "This SeqList is Empty !" << endl;
                    return;
           }
           else
           {
                    _array[--_size]=NULL ;
           }
}

void SeqList::PushFront(const DataType & x)   //头插
{
           if (_size == 0)
           {
                    PushBack(x );
                    return;
           }
           else
           {
                    CheckCapacity();
                    int key = _size;
                    while (key)
                    {
                              _array[key--] = _array[key - 1];
                    }
                    _array[0] = x;
                    _size++;
           }
}
void SeqList::PopFront()  //头删
{
           if (_size == 0||_size==1)
           {
                    PopBack();
           }
           else
           {
                    for (int i = 0; i < _size-1;i++)
                    {
                              _array[i] = _array[i + 1];
                    }
                    --_size;
           }
}
void SeqList::Insert(size_t pos , const DataType& x)   //固定位置插入数据
{
           assert( pos<_size);    //需检验pos的合法性
           CheckCapacity();
           if (pos == _size - 1)   //在最后一个位置插入数据等于尾插
           {
                    PushBack(x );
                    return;
           }
           else
           {
                    for (int i = _size; i > pos; --i)
                    {
                              _array[i] = _array[i - 1];
                    }
                    _array[pos ] = x ;
                    _size++;
           }
}

int SeqList::Find(const DataType & x)    //查找数据
{
           assert(_array != NULL);
           for (int i = 0; i < _size; i++)
           {
                    if (_array[i] == x )
                              return i;
           }
           return -1;
}
void SeqList::Erase(size_t pos )     //固定位置删除数据
{
           assert(_array!= NULL);
           assert( pos < _size);
           if (pos == _size - 1)
           {
                    PopBack();
                    return;
           }
           if (pos == 0)
           {
                    PopFront();
                    return;
           }
           for (int i = pos; i < _size-1; i++)
           {
                    _array[i] = _array[i + 1];
           }
           --_size;
}
int  SeqList::Remove(const DataType & x)   //删除x
{
           assert(_array);
           int pos=Find(x );
           if (pos != -1)
           {
                    Erase(pos);
                    return 1;
           }
           else
           {
                    return -1;
           }
                   
}

//测试用例
//void Test1()
//{
//   SeqList list1;
//   list1.PushBack(1);
//   list1.PushBack(2);
//   list1.PushBack(3);
//   list1.PushBack(4);
//   list1.PushBack(5);
//
//   list1.Print();
//
//   SeqList list2;
//   list2.PushBack(0);
//   list2.PushBack(9);
//   list2.PushBack(8);
//   list2.PushBack(7);
//   list2.PushBack(6);
//   list2.Print();
//
//   list1 = list2;
//   list1.Print();
//
//   SeqList list3(list1);
//   list3.Print();
//}

void Test2()
{
           SeqList list1;

           //list1.PushFront(0);
           //list1.Print();

           list1.PushBack(1);
           list1.PushBack(2);
           list1.PushBack(3);
           list1.PushBack(4);
           list1.PushBack(5);
           list1.Print();

           //list1.PopFront();
           //list1.Print();
           /*list1.Insert(2, 0);
    list1.Print();*/
           //cout <<"该数字出现在:_array["<< list1.Find(5) <<"]"<< endl;
           //list1.Erase(2);
          
           int ret=list1.Remove(7);
           if (ret == -1)
           {
                    cout << "not exit !" << endl;
           }
           else
           {
                    list1.Print();
           }
}
int main()
{
           //Test1();
           Test2();
           system("pause" );
           return 0;
}


本文出自 “言安阳” 博客,请务必保留此出处http://iynu17.blog.51cto.com/10734157/1752354

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用c++实现顺序表:多文件编程,层次清晰,函数有注释 SeqList();//构造函数,存储的元素个数设为0 bool setLength(size_t length);//设置已经存储的元素个数 bool addElement(ElemType element);//把某个元素添加到顺序表末尾 bool addElement(ElemType element , size_t n);//插入一个元素,使其成为第n个元素,其余元素后移 bool delElement();//删除所有的元素 bool delElement(size_t n);//删除第n个元素 bool delElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,把这个元素删除 bool replaceElement(ElemType element , size_t n);//使用一个元素,替换掉第n个元素 bool swapElement(size_t n1 , size_t n2);//把第n1个元素和第n2个元素交换 ElemType* getElement();//得到数组头的指针 ElemType* getElement(size_t n);//得到第n个元素的指针 size_t getLength();//得到存储的元素个数 size_t getMaxSize();//得到顺序表容量 bool showElementDetail();//输出所有的元素细节 bool showElementDetail(size_t n);//输出第n个元素的细节 bool showElementDetail(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,输出元素所有细节 size_t findElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素位置 static int inputAInt(int min = 0,int max = 9,int defaultValue = -1);//从键盘读取,限制为一个min到max间的整数,非法情况返回defaultValue void startControlLoop();//打开控制界面 ~SeqList();//析构函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值