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

#include <iostream>
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<<"表内没有数据!"<<endl ;
   return  0;
 }
 if (i>maxIndex)
 {    
  
  cout<<"超出最大长度"<<endl ;
  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<<"线性表已满!"<<endl ;
  return this ;
   }  
   if ((position>maxIndex+1||position==maxIndex+1)&&position<length) //尾部
   {
    pLinerTableHeader[maxIndex+1]=data ;
    maxIndex++ ;
    return  this;
   }
   int x,y ;
   for (int i=position;i<maxIndex;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<<"表满!"<<endl ;
    return this;
   }
   pLinerTableHeader[maxIndex+1] =data ;
   maxIndex++ ;
   return this ;
}

ArrayLinerTable*ArrayLinerTable::PrintLinerTable()

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

ArrayLinerTable*  ArrayLinerTable::Delete(int position)
{  
    if(position>maxIndex){
  cout<<"位置超过最大索引"<<endl ;
  return this ;
 }
 if(position==maxIndex){  //尾部删除
  maxIndex-- ;
  return this ;
 } 
   for(int i=position;i<maxIndex;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<maxIndex;i++)
   {
    pLinerTableHeader[i]=0 ;
   
   }
   maxIndex=-1 ;
   return this ;
}

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

}

ArrayLinerTable* ArrayLinerTable::SortAsc() //升序
{
    for (int i=0;i<=maxIndex-1;i++)
    {
  for (int j=i+1;j<=maxIndex;j++)
  {
   if (pLinerTableHeader[i]>pLinerTableHeader[j])
   {
               pLinerTableHeader[i]=pLinerTableHeader[j]+pLinerTableHeader[i] ;
      pLinerTableHeader[j]=pLinerTableHeader[i]-pLinerTableHeader[j] ;
      pLinerTableHeader[i]=pLinerTableHeader[i]-pLinerTableHeader[j] ;
   }

  }
    }
 return this ;
}
ArrayLinerTable* ArrayLinerTable::SortDesc() //降序
{

    for (int i=0;i<=maxIndex-1;i++)
    {
  for (int j=i+1;j<=maxIndex;j++)
  {
   if (pLinerTableHeader[i]<pLinerTableHeader[j])
   {
    pLinerTableHeader[i]=pLinerTableHeader[j]+pLinerTableHeader[i] ;
    pLinerTableHeader[j]=pLinerTableHeader[i]-pLinerTableHeader[j] ;
    pLinerTableHeader[i]=pLinerTableHeader[i]-pLinerTableHeader[j] ;
   }
   
  }
    }
 return this ;
}
ArrayLinerTable::~ArrayLinerTable()

   if(pLinerTableHeader!=NULL)
    delete this->pLinerTableHeader;
   cout<<"对象释放!"<<endl ;
}

 

 

 

#include <iostream>
#include "ArrayLinerTable.h"
using namespace std; 
int main(int*argc,char **agrgv)
{
    
   //cout<<__FILE__<<"---"<<__LINE__<<endl; 显示行 文件
   //预编译指令用于防止某些代码被编译  通常被用作调试使用
#ifdef DEBUG  
   cout<<"开启DEBUG模式!"<<endl ;
#endif
   //system("COLOR C9") ;
   ArrayLinerTable *linerTable=new ArrayLinerTable() ;
   for(int i=0;i<10;i++)
    linerTable->AppendTo(i) ;
   linerTable->Insert(1,15) ;
   linerTable->PrintLinerTable();
   cout<<"--------升序排序---------"<<endl ;
   linerTable->SortAsc()->PrintLinerTable() ;
   cout<<"-------降序排序----------"<<endl ;
   linerTable->SortDesc()->PrintLinerTable() ;
   cout<<"-------清空数据----------"<<endl ;
   linerTable->ClearAllData()->PrintLinerTable()->MakeEmpty();
   delete linerTable ;
 return    0;
}

转载于:https://www.cnblogs.com/yuedongwei/p/4145419.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值