#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include<assert.h>
using namespace std;
class Seqlist
{
public:
Seqlist()
:_a(NULL),_size(1),_capacity(1)
{ }
Seqlist(const Seqlist& s)
{
if (this->_a)
{
/*_a=(int*)malloc(sizeof(int)*s._size);*/
_a=new int[];
memcpy(_a,s._a,sizeof(int)*s._size);
}
else
{
_a=NULL;
}
}
~Seqlist()
{
delete [] _a ;
cout<<"~Seqlist()"<<endl;
}
void Insert(size_t pos, int x)
{
_CheckCapacity();
size_t end= _size;
while(end>=pos)
{
_a[end+1]=_a[end];
--end;
}
_a[pos]=x;
++_size;
}
void PushBack(int x)
{
_CheckCapacity();
_a[_size]=x;
++_size;
}
void PushFront(int x)
{
_CheckCapacity();
size_t end= _size;
while (end-->1)
{
_a[end+1]=_a[end];
}
_a[1]=x;
++_size;
}
void Erase(size_t pos)
{
assert(pos<=_size);
size_t end= _size;
while(end>=pos)
{
_a[pos]=_a[pos+1];
pos++;
}
--_size;
}
int Find(int x)
{
for (int j=0;j<=(int)_size;j++)
{
if(x==_a[j])
{
return 1;
}
}
return -1;
}
void _CheckCapacity()
{
if (_size==_capacity)
{
_capacity=_capacity*2+3;
_a=(int*)realloc(_a,_capacity*sizeof(int));
}
}
void Print()
{
for (size_t i=1;i<_size;++i)
{
cout<<_a[i]<<" ";
}
cout<<endl;
}
private:
int* _a;
size_t _size;
size_t _capacity;
};
#include"Seqlist.h"
void test()
{
Seqlist s;
s.Insert(1,1);//测试
s.Insert(1,2);
s.Insert(1,3);
s.Insert(1,4);
s.Insert(1,5);
s.Print();
// 找元素
if (s.Find(0)==1)
{
cout<<"找到"<<endl;
}
else
{
cout<<"没有找到"<<endl;
}
//删除
s.Erase(2);
s.Print();
//尾插
s.PushBack(0);
s.Print();
//头插
s.PushFront(10);
s.Print();
}
int main()
{
test();
return 0;
}