C++数据结构和算法每天一练(线性表)
2013-06-21 14:16
423 查看
#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;
}
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;
}
相关文章推荐
- C++数据结构和算法每天一练(线性表)
- 数据结构和算法 C/C++ Java 和 C# 版 - (2)线性表 精准表述 实现
- 【算法和数据结构】线性表(一)线性表(C++实现)
- 【源代码】C++实现严蔚敏数据结构所有算法(一)线性表-顺序表
- 数据结构和算法 C/C++ Java 和 C# 版 - (1)线性表 精准表述
- 【算法和数据结构】_3_线性结构_栈
- 【数据结构&&等差数列】KMP简介和算法的实现(c++ && java)
- C++代码,数据结构-最小生成树的两个算法,Prime&Kruskal
- 数据结构与算法之线性表
- 渣渣小本求职复习之路每天一博客系列——数据结构与常用算法(3)
- 算法导论 第8章 线性时间排序 C++实现
- 算法与数据结构课程中的c++实现的顺序表和链表
- 线性表10 - 数据结构和算法15
- 数据结构与算法之贪心算法 C++实现
- 【数据结构&&等差数列】KMP简介和算法的实现(c++ && java)
- 【算法和数据结构】排序(四)归并排序和快速排序(C++实现)
- 【刘庆源码共享】稀疏线性系统求解算法MGMRES(m) 之 矩阵类定义一(C++)
- 算法与数据结构基础10:C++实现——拓扑排序
- 算法与数据结构--实现线性表的插入操作--算法2.3
- 【算法和数据结构】图(二)最小生成树之Prim算法(C++实现)