标签:c++实现顺序表
顺序表可看成一位数组。
#include <iostream>
using namespace std;
typedef int DataType;
#define MAX_CAPACITY 3
class Seqlist
{
friend ostream& operator<<(ostream& os,Seqlist& x);
public:
Seqlist(DataType capacity = MAX_CAPACITY)//构造顺序表
:_size(0)
,_pdata(0)
,_capacity(capacity)
{
_pdata = new DataType[MAX_CAPACITY];
}
~Seqlist()
{
if(_pdata != NULL)
{
delete[] _pdata;
}
}
Seqlist(const Seqlist& x)//拷贝构造
{
_size = x._size;
_capacity = x._capacity;
_pdata = new DataType[_capacity];
memcpy(_pdata,x._pdata,_size*sizeof(DataType));
}
Seqlist& operator=(const Seqlist& x)//赋值函数
{
if(this != &x)
{
delete[] _pdata;
_pdata = new DataType[x._capacity];
_size = x._size;
_capacity = x._capacity;
memcpy(_pdata,x._pdata,_size*sizeof(DataType));
}
return *this;
}
public:
void CheckCapacity()//检查容量
{
if(_size == _capacity)
{
DataType* tmp = new DataType[_capacity*2];
memcpy(tmp,_pdata,_size*sizeof(DataType));
delete[] _pdata;
_pdata = tmp;
_capacity *= 2;
}
}
void PushBack(const DataType& x)//后插
{
CheckCapacity();
_pdata[_size++] = x;
}
void PushFront(const DataType& x)//前插
{
CheckCapacity();
DataType i = 0;
for(i=_size-1;i>=0;i--)
{
_pdata[i+1] = _pdata[i];
}
_pdata[0] = x;
_size++;
}
void PopBack()//后删
{
_size--;
}
void PopFront()//前删
{
DataType i = 0;
for(i=0;i<_size;i++)
{
_pdata[i] = _pdata[i+1];
}
_size--;
}
int Find(const DataType& x)//查找某一元素
{
DataType i = 0;
if(_pdata == NULL)
{
cout<<"表空"<<endl;
}
else
{
for(i=0;i<_size;i++)
{
if(_pdata[i] == x)
{
return i;
}
}
return -1;
}
}
void Insert(const DataType& pos,const DataType& x)//在某一位置插入一个元素
{
CheckCapacity();
DataType i = 0;
if((pos>0)||(pos<=_size+1))
{
for(i=_size-1;i>=pos-1;i--)
{
_pdata[i+1] = _pdata[i];
}
_pdata[pos-1] = x;
_size++;
}
}
void Remove(const DataType& x)
{
DataType i = 0;
DataType j = 0;
for(i=0;i<_size;i++)
{
if(_pdata[i] == x)
{
for(j=i;j<_size;j++)
{
_pdata[j] = _pdata[j+1];
}
_size--; //删除所有x
//break; 删除一个x
}
}
}
void Reverse(Seqlist& x)//逆置
{
DataType tmp = 0;
DataType left = 0;
DataType right = x._size-1;
while(left<right)
{
tmp = x._pdata[left];
x._pdata[left] = x._pdata[right];
x._pdata[right] = tmp;
left++;
right--;
}
}
void operator[](const DataType& index)
{
if((index>=0)||(index<_size))
{
_pdata[index] = 8;
}
}
private:
DataType* _pdata;
DataType _size;//当前元素的个数
DataType _capacity;//容量
};
ostream& operator<<(ostream& os,Seqlist& x)
{
DataType i = 0;
for(i=0;i<x._size;i++)
{
os<<x._pdata[i]<<" ";
}
os<<endl;
return os;
}
void Test1()
{
Seqlist seqlist1;
/*seqlist1.PushBack(1);
seqlist1.PushBack(2);
seqlist1.PushBack(3);
seqlist1.PushBack(4);
seqlist1.PushBack(5);
seqlist1.PopBack();
cout<<seqlist1;*/
seqlist1.PushFront(1);
seqlist1.PushFront(2);
seqlist1.PushFront(3);
seqlist1.PopFront();
cout<<seqlist1;
}
//void Test2()
//{
// Seqlist seqlist1;
// seqlist1.PushBack(1);
// seqlist1.PushBack(2);
// seqlist1.PushBack(3);
// seqlist1.PushFront(4);
// seqlist1.PushFront(5);
// cout<<seqlist1;
// Seqlist seqlist2(seqlist1);
// cout<<seqlist2;
// Seqlist seqlist3;
// seqlist3 = seqlist2;
// cout<<seqlist3;
//}
void Test3()
{
Seqlist seqlist1;
int ret = 0;
seqlist1.PushBack(1);
seqlist1.PushBack(2);
seqlist1.PushBack(3);
seqlist1.PushFront(4);
seqlist1.PushFront(2);
cout<<seqlist1;
//ret = seqlist1.Find(6);
//if(ret != -1)
//{
// cout<<ret<<endl;
//}
//else
//{
// cout<<"表中无此元素"<<endl;
//}
seqlist1.Insert(6,6);
cout<<seqlist1;
//seqlist1.Remove(2);
//seqlist1.Reverse(seqlist1);
seqlist1.operator [](3);
cout<<seqlist1;
}
int main()
{
Test3();
return 0;
}
标签:c++实现顺序表
原文地址:http://10810429.blog.51cto.com/10800429/1751866