#include <iostream>using namespace std; #include <assert.h>typedef int DataType; class seqlist//when copying the copy, be sure to note the direction of the replication {public:seqlist (): _array (NULL), _size (0), _capacity (0) {}seqlist ( Datatype* array, size_t size)//For the modern wording of the copy construction is overloaded: _array (New datatype[size]), _size (size ), _capacity (size) {memcpy (_array, array, sizeof (DataType) *size);} ~seqlist () {if (_array) {/*delete[] _array;*/free (_array);//free to parentheses}}//1, dark-copy traditional notation/*seqlist (const seqlist& s): _array (New datatype[s._size]), _capacity (s._size), _size (s._size) { memcpy (_array, s._array, sizeof (DataType) *_size);} Seqlist& operator= (const seqlist& s) {if (this != &s) {DataType* Tmp = new datatype[s._size];d elete[] _array;_array = tmp;_size = s._ size;_capacity = s._size;memcpy (_array, s._array, sizeof (DataType) *_size);} Return (*this);} *///2, modern notation Seqlist (const seqlist& s)//a function in addition to the function overload can not have the same functions: _array (NULL) {seqlist tmp (S._array, s._size); Std::swap (_array, tmp._array); _size = s._size;_capacity = _size;} Seqlist operator= ( seqlist s) {std::swap (_array, s._array); _size = s._size;_ capacity = s._capacity;//not for _size because of itself opened _capacity Space return (*this);} Public:void pushback (datatype x); Void popback (); Void pushfront (DataType x);void Popfront (); Void insert (size_t pos, datatype x);//focus, error prone void erase (size_t pos); Int find (datatype x), void printseqlist ();p rivate:void _checkcapacity () {if (_size >= _capacity) {_capacity = 2 * _capacity + 3;_array = ( datatype*) realloc (_array, _capacity*sizeof (DataType));}} private:datatype* _array;size_t _size;size_t _capacity;}; Void seqlist::P USHBACK (datatype x) {_checkcapacity (); _array[_size++] = x;} Void seqlist::P opback () {--_size;} Void seqlist::P ushfront (DATATYPE&NBSP;X)//Remember to copy from backward {_checkcapacity ();for (size_t i = _size; i >= 1; --i) {_array[i] = _array[i-1];} _array[0] = x;++_size;} Void seqlist::P Opfront () {for (size_t i = 0; i < _size; + +i) {_array[i] = _array[i + 1];} --_size;} Void seqlist::insert (SIZE_T&NBSP;POS,&NBSP;DATATYPE&NBSP;X)//did not make too much judgment for Pos{assert (pos > 0 && pos <= _size);//With Assert _checkcapacity ();//for (size_t i = &NBSP;_SIZE-1;&NBSP;I&NBSP;>=&NBSP;POS;&NBSP;--I)//error, pos==0 should be for Dead loop, forever greater than 0//{//_array[i+1] = _array [i];//}for (size_t i = _size; i > pos; --i)//So the size_t type has 0, it is best to use left open right closed interval , i.e. [) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBWhen the sp; //size_t variable is used as a cyclic condition, This will take less than 0 of the interval _array[i] = _array[i-1];} _array[pos] = x;++_size;} Void seqlist::erase (Size_t pos)//For unsigned numbers, passing in negative numbers becomes a large number, so that the assertion does not reach the purpose {assert (pos >= 0 & & pos < _size);for (int i = pos; i < _size; + +i) {_array[i] = _array[i + 1];} --_size;} Int seqlist::find (DATATYPE&NBSP;X)///return value must be set to int is not used, because to find to output -1{for (int i = 0; i < _size; ++i) {if (_array[i] == x) {return i;}} Return -1;} Void seqlist::P rintseqlist () {for (size_t i = 0; i < _size; ++i) {cout << _array[i] << " ";} Cout << endl;}
C + + implementation sequence table