C++数据结构和算法每天一练(线性表) (一)

2014-11-24 00:40:33 · 作者: · 浏览: 9

#include
using namespace std;
class ArrayLinerTable
{
public:
void InitLinerTable(int); //初始化线性表
void MakeEmpty() ;//清空线性表
int GetLength() ;//获取长度
int Locate(int) ;//获取制定位置的数据
ArrayLinerTable* Insert(int,int) ;//在制定位置插入一个元素
ArrayLinerTable* AppendTo(int) ;//追加
ArrayLinerTable* PrintLinerTable() ;//打印线性表
ArrayLinerTable* Delete(int) ;//删除指定位置
ArrayLinerTable* Update(int,int) ;//修改元素
bool IsEmpty() ;//是否空
ArrayLinerTable*ClearAllData() ;//清除所有元素
ArrayLinerTable*SortAsc() ;//升序
ArrayLinerTable*SortDesc();//降序
ArrayLinerTable(); //构造
~ArrayLinerTable();//析构
private:
int *pLinerTableHeader ; //表头
int length ;//
int maxIndex ;//当前最大数量

};
ArrayLinerTable::ArrayLinerTable()
{
this->maxIndex= -1; //刚开始不存在-1
pLinerTableHeader=NULL ;
}
void ArrayLinerTable::InitLinerTable(int length)//不能使用模版成员指针 因为在编译期间无法确定类型所以是错误的
{
this->pLinerTableHeader=new int[length] ;
this->length=length ;
}
void ArrayLinerTable::MakeEmpty()
{
delete this->pLinerTableHeader ;
this->pLinerTableHeader=NULL ;
}

int ArrayLinerTable::GetLength()
{
return this->length ;

}
int ArrayLinerTable::Locate(int i)
{
if(-1==maxIndex)
{
cout<<"表内没有数据!"< return 0;
}
if (i>maxIndex)
{

cout<<"超出最大长度"< return 0;
}
return pLinerTableHeader[i] ;
}

ArrayLinerTable* ArrayLinerTable::Insert(int position,int data)
{
if(pLinerTableHeader==NULL)
{
InitLinerTable(100) ;//初始化
this->length=100 ;
this->maxIndex=0 ;
pLinerTableHeader[0]=data ;
return this ;
}
if(maxIndex>=length-1)
{
cout<<"线性表已满!"< return this ;
}
if ((position>maxIndex+1||position==maxIndex+1)&&position {
pLinerTableHeader[maxIndex+1]=data ;
maxIndex++ ;
return this;
}
int x,y ;
for (int i=position;i {
if(i==position) //第一次的叫唤
{
x=pLinerTableHeader[i+1] ;
pLinerTableHeader[i+1]=pLinerTableHeader[position] ;
continue;
}
y=pLinerTableHeader[i+1];
pLinerTableHeader[i+1]=x ;
x=y ;
}
pLinerTableHeader[maxIndex+1]=x;
pLinerTableHeader[position]=data ;
maxIndex++ ;
return this ;
}

ArrayLinerTable* ArrayLinerTable::AppendTo(int data)
{
if(pLinerTableHeader==NULL)
{
InitLinerTable(100) ;//初始化
this->length=100 ;
this->maxIndex=0 ;
pLinerTableHeader[0]=data ;
return this ;
}
if (maxIndex==length-1)
{
cout<<"表满!"< return this;
}
pLinerTableHeader[maxIndex+1] =data ;
maxIndex++ ;
return this ;
}

ArrayLinerTable*ArrayLinerTable::PrintLinerTable()
{
if (maxIndex==-1)
{
cout<<"No Data"< }
for (int i=0;i<=maxIndex;i++)
{
cout<<"Position "<Locate(i)< }
return this;
}

ArrayLinerTable* ArrayLinerTable::Delete(int position)
{
if(position>maxIndex){
cout<<"位置超过最大索引"< return this ;
}
if(position==maxIndex){ //尾部删除
maxIndex-- ;
return this ;
}
for(int i=position;i {
pLinerTableHeader[i]=pLinerTableHeader[i+1];
}
maxIndex--;
return this ;
}
bool ArrayLinerTable::IsEmpty()
{
return this->pLinerTableHeader==NULL true:false ;
}
ArrayLinerTable*ArrayLinerTable::ClearAllData()
{
for (int i=0;i {
pLinerTableHeader[i]=0 ;

}
maxIndex=-1 ;
return this ;
}

ArrayLinerTable* ArrayLinerTable::Update(int position,int data)
{
if(position>maxIndex){
cout<<"位置超过最大索引"< return this ;
}
pLinerTableHeader[position]=data ;
return this ;

}

ArrayLinerTable* ArrayLinerTable::SortAsc() //升序
{
for (int i=0;i<=maxIndex-1;i++)
{
for (in