C++实现动态顺序表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);

           ~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 ;
}
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);
           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)
{
           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

以上是关于C++实现动态顺序表的主要内容,如果未能解决你的问题,请参考以下文章

C++实现动态顺序表的PushBack(),PopBack(),PushFront(),PopFront(),Find(),Insert

函数模板在c++动态顺序表中的大作用

C++数据结构——顺序表(基本代码实现与案例)

C++ class实现静态分配顺序表(完整代码)

用C++实现顺序表

C++学习(二十九)(C语言部分)之 顺序表