#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
typedef int DataType;
#define DEFAULT_INC 9
#define DEFAULT_CAPACITY 7
class SeqList
{
public:
friend ostream& operator<<(ostream& os,
const SeqList& s);
SeqList(int capacity = DEFAULT_CAPACITY)
:_capacity(capacity)
, _size(0)
, _pdata(0)
{
_pdata = new DataType[capacity];;
}
SeqList(const SeqList& s)
:_size(s._size)
, _capacity(s._capacity)
{
_pdata = new DataType[_capacity];
memcpy(_pdata, s._pdata, _size*sizeof
(DataType));
}
void PushBack( const DataType& d)
{
_CheckCapacity();
_pdata[_size++] = d;
}
void PushFront( const DataType& d)
{
_CheckCapacity();
int start = _size - 1;
while (start >= 0)
{
_pdata[start + 1] = _pdata[start];
start--;
}
_pdata[0] = d;
_size++;
}
SeqList& operator=(const SeqList& s)
{
if (this != &s)
{
delete[] _pdata;
_pdata = new DataType[s._capacity];
memcpy(_pdata, s._pdata, s._size*sizeof
(DataType));
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
~SeqList()
{
if (_pdata != NULL)
{
delete[]_pdata;
}
}
public:
void _CheckCapacity()
{
if (_size == _capacity)
{
DataType* tmp = new DataType[_capacity
+DEFAULT_INC];
memcpy(tmp, _pdata, _size*sizeof(DataType));
delete[] _pdata;
_pdata = tmp;
_capacity = _capacity + DEFAULT_INC;
}
}
private:
DataType* _pdata;
int _size;//
int _capacity;//
};
ostream& operator<<(ostream& os, const SeqList& s)
{
int i = 0;
for (i = 0; i < s._size; i++)
{
os << s._pdata[i] << " ";
}
os << endl;
return os;
}
int main()
{
SeqList seqlist1;
seqlist1.PushBack(1);
seqlist1.PushBack(2);
seqlist1.PushBack(3);
seqlist1.PushBack(4);
seqlist1.PushBack(5);
seqlist1.PushBack(6);
seqlist1.PushBack(7);
seqlist1.PushBack(8);
cout << seqlist1;
SeqList seqlist2(seqlist1);
cout << seqlist2;
SeqList seqlist3;
seqlist3 = seqlist2;
cout << seqlist3;
system("pause");
return 0;
}