#include<iostream>
#include<assert.h>
using namespace std;
//typedef struct FindRet
//{
// bool isFind; // 是否找到的标示
// size_t index; // 找到数据的下标
//}Findret;
typedef int DataType;
class SeqList
{
public:
SeqList()
{
_array = new int[10];
_size = 0;
_capicity = 10;
}
SeqList(const SeqList& sList)
{
_array = new int [sList._capicity] ;
_size = sList._size ;
_capicity = sList._capicity ;
}
SeqList& operator=(const SeqList& sList)
{
if(this != &sList)
{
delete []_array;
_array = new int [sList._capicity];
_size = sList._size ;
_capicity = sList._capicity ;
}
return *this;
}
~SeqList()
{
delete []_array;
}
void PushBack(const DataType& x) //尾插
{
_CheakCapicity();
_array [_size] = x;
_size ++;
}
void PushFront(const DataType& x)//头插
{
_CheakCapicity();
int i=_size;
for(i=_size; i > 0; i--)
{
_array[i] = _array[i-1];
}
_array[0] = x;
_size++;
}
void PopFront()//头删
{
if(_size == 0)
{
cout<<"顺序表为空"<<endl;
}
else
{
int count = 1;
for(count = 1; count<_size; count++)
{
_array[count-1] = _array[count];
}
_array[_size - 1] = 0;
_size--;
}
}
void PopBack()
{
if(_size == 0)
{
cout<<"顺序表为空"<<endl;
}
else
{
_array[_size - 1] = 0;
_size--;
}
}
void Print()
{
int count = _size;
for(_size= 0; _size < count ; _size++)
{
cout<<_array[_size]<<" ";
}
cout<<endl;
}
// 实现上面的函数
void Insert(size_t index, const DataType& x)
{
_CheakCapicity();
int i = _size;
for(i = _size; i >= index; i--)
{
_array[i]= _array[i-1];
}
_array[index-1]=x;
_size++;
}
void Modified (size_t index, const DataType& x)
{
if(index < 0 && index >_size -1)
{
cout<<"不合法的位置"<<endl;
}
else
{
_array[index - 1] = x;
}
}
void Remove(size_t index)
{
if(index < 0 && index >_size -1)
{
cout<<"不合法的位置"<<endl;
}
else
{
int count = index;
for(count = index; count <=_size - 1; count++)
{
_array[count-1]=_array[count];
}
_array[_size-1] = 0;
_size--;
}
}
bool Find(const DataType& x, size_t findIndex)
{
if(findIndex <0||findIndex >_size-1)
{
return false;
}
if(_array[findIndex]==x)
{
return true;
}
return false;
}
private:
bool _CheakCapicity()
{
if(_size == _capicity)
{
DataType* tmp = new DataType [_capicity * 2];
memcpy(tmp,_array,sizeof(_size * 4));
delete []_array;
_array = tmp;
return true;
}
}
private:
DataType* _array;
size_t _size;
size_t _capicity;
};
int main()
{
SeqList s1;
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.PushBack(5);
s1.PushFront(6);
s1.PushFront(7);
s1.PushFront(8);
s1.Insert(3,2);
s1.Modified(3,7);
s1.Remove(3);
s1.PopFront();
s1.PopBack();
s1.Print ();
getchar();
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/liuxiaoqian_/article/details/47983507