C++实现
#include <iostream>
using namespace std;
//结构的定义部分
const int INITSIZE=3 ;
const int INCREMENT=1 ;
typedef int ElemType;
typedef struct {
ElemType * elem;
int length;
int listsize;
} SqList;
//结构的生成操作
void InitList(SqList &L,int maxsize=0 )
{
if(maxsize==0) maxsize= INITSIZE;
L.elem=new ElemType[maxsize];
if(!L.elem) exit(1 );
L.length=0 ;
L.listsize= maxsize;
}
//结构的销毁操作
void DestroyList(SqList & L)
{
delete [] L.elem;
L.elem= NULL;
L.length=0 ;
L.listsize=0 ;
}
//结构的清理操作
void ClearList(SqList & L)
{
L.length=0 ;
}
//结构的状态查看
bool ListEmpty(SqList L)
{
return L.length==0 ;
}
int ListLength(SqList L)
{
return L.length;
}
void ListOutput(SqList L)
{
cout<<"(" ;
for(int i=0;i<L.length-1;i++) cout<<L.elem[i]<<"," ;
if(L.length-1>=0) cout<<L.elem[L.length-1 ];
cout<<")"<< endl;
}
void ListInfo(SqList L)
{
cout<<" " ;
ListOutput(L);
if(ListEmpty(L)) cout<<" Empty:Yes"<< endl;
else cout<<" Empty:No"<< endl;
cout<<" Length="<<ListLength(L)<<",Size="<<L.listsize<< endl;
}
//结构的查改增删
bool GetElem(SqList L,int pos,ElemType & e)
{
if(pos<1||pos>L.length) return false ;
e=L.elem[pos-1 ];
return true ;
}
bool ElemEqual(ElemType e1,ElemType e2)
{
return e1== e2;
}
bool ElemPlus(ElemType e1,ElemType e2)
{
return e1> e2;
}
bool ElemMinus(ElemType e1,ElemType e2)
{
return e1< e2;
}
int LocateElem(SqList L,ElemType e,bool (* compare)(ElemType,ElemType))
{
for(int i=1;i<=L.length;i++ )
if((*compare)(L.elem[i-1],e)) return i;
return 0 ;
}
bool PutElem(SqList &L,int pos,ElemType e)
{
if(pos<1||pos>L.length) return false ;
L.elem[pos-1]= e;
return true ;
}
bool ListInsert(SqList &L,int pos,ElemType e)
{
if(pos<1||pos>L.length+1) return false ;
if(L.length>= L.listsize)
{
ElemType *newbase=new ElemType[L.listsize+ INCREMENT];
if(!newbase) exit(1 );
for(int i=0;i<L.length;i++) newbase[i]= L.elem[i];
delete [] L.elem;
L.elem= newbase;
L.listsize+= INCREMENT;
}
for(int i=L.length-1;i>=pos-1;i-- )
L.elem[i+1]= L.elem[i];
L.elem[pos-1]= e;
L.length++ ;
return true ;
}
bool ListDelete(SqList &L,int pos,ElemType & e)
{
if(pos<1||pos>L.length) return false ;
for(int i=pos;i<L.length;i++ )
L.elem[i-1]= L.elem[i];
e=L.elem[pos-1 ];
L.length-- ;
return true ;
}
//结构的排序操作
void ListSort(SqList & L)
{
for(int i=0;i<L.length-1;i++ )
for(int j=0;j<L.length-1-i;j++ )
if(L.elem[j]>L.elem[j+1 ])
{
int tmp= L.elem[j];
L.elem[j]=L.elem[j+1 ];
L.elem[j+1]= tmp;
}
}
void MergeList(SqList La,SqList Lb,SqList & Lc)
{
ElemType *pa,*pb,*pc,*pa_last,* pb_last;
pa = La.elem; pb = Lb.elem;
Lc.listsize = Lc.length = La.length+ Lb.length;
pc = Lc.elem = new ElemType[Lc.listsize];
if (!Lc.elem) exit(1);
pa_last = La.elem+La.length-1 ;
pb_last = Lb.elem+Lb.length-1 ;
while(pa<=pa_last&&pb<=pb_last){
if (*pa<=*pb) *pc++=*pa++ ;
else *pc++=*pb++ ;
}
while (pa<=pa_last) *pc++=*pa++;
while (pb<=pb_last) *pc++=*pb++;
}
//结构的功能测试
int main()
{
cout<<"顺序表结构的测试:"<< endl;
SqList A;
cout<<"***************************************"<< endl;
cout<<"生成操作:InitList(A)"<< endl;
InitList(A);
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"插入操作:ListInsert(A,1,2)"<< endl;
ListInsert(A,1,2 );
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"插入操作:ListInsert(A,2,7)"<< endl;
ListInsert(A,2,7 );
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"插入操作:ListInsert(A,2,8)"<< endl;
ListInsert(A,2,8 );
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"排序操作:ListSort(A)"<< endl;
ListSort(A);
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"插入操作:ListInsert(A,2,21)"<< endl;
ListInsert(A,2,21 );
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"插入操作:ListInsert(A,4,15)"<< endl;
ListInsert(A,4,15 );
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"删除操作:ListDelete(A,5,e)"<< endl;
ElemType e;
ListDelete(A,5 ,e);
ListInfo(A);
cout<<" e="<<e<< endl;
cout<<"***************************************"<< endl;
cout<<"排序操作:ListSort(A)"<< endl;
ListSort(A);
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"更新操作:PutElem(A,4,39)"<< endl;
PutElem(A,4,39 );
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"查找操作:GetElem(A,2,e)"<< endl;
GetElem(A,2 ,e);
ListInfo(A);
cout<<" e="<<e<< endl;
cout<<"***************************************"<< endl;
cout<<"定位操作:LocateElem(A,15,ElemEqual)"<< endl;
int Pos=LocateElem(A,15 ,ElemEqual);
ListInfo(A);
cout<<" Pos="<<Pos<< endl;
cout<<"***************************************"<< endl;
cout<<"定位操作:LocateElem(A,6,ElemPlus)"<< endl;
Pos=LocateElem(A,6 ,ElemPlus);
ListInfo(A);
cout<<" Pos="<<Pos<< endl;
cout<<"***************************************"<< endl;
cout<<"定位操作:LocateElem(A,8,ElemMinus)"<< endl;
Pos=LocateElem(A,8 ,ElemMinus);
ListInfo(A);
cout<<" Pos="<<Pos<< endl;
cout<<"***************************************"<< endl;
SqList B;
InitList(B);
ListInsert(B,1,9 );
ListInfo(B);
cout<<"***************************************"<< endl;
SqList C;
InitList(C);
MergeList(A,B,C);
ListOutput(A);
ListOutput(B);
ListOutput(C);
cout<<"按任意键,结束..." ;
cin.get ();
return 0 ;
}
using namespace std;
//结构的定义部分
const int INITSIZE=3 ;
const int INCREMENT=1 ;
typedef int ElemType;
typedef struct {
ElemType * elem;
int length;
int listsize;
} SqList;
//结构的生成操作
void InitList(SqList &L,int maxsize=0 )
{
if(maxsize==0) maxsize= INITSIZE;
L.elem=new ElemType[maxsize];
if(!L.elem) exit(1 );
L.length=0 ;
L.listsize= maxsize;
}
//结构的销毁操作
void DestroyList(SqList & L)
{
delete [] L.elem;
L.elem= NULL;
L.length=0 ;
L.listsize=0 ;
}
//结构的清理操作
void ClearList(SqList & L)
{
L.length=0 ;
}
//结构的状态查看
bool ListEmpty(SqList L)
{
return L.length==0 ;
}
int ListLength(SqList L)
{
return L.length;
}
void ListOutput(SqList L)
{
cout<<"(" ;
for(int i=0;i<L.length-1;i++) cout<<L.elem[i]<<"," ;
if(L.length-1>=0) cout<<L.elem[L.length-1 ];
cout<<")"<< endl;
}
void ListInfo(SqList L)
{
cout<<" " ;
ListOutput(L);
if(ListEmpty(L)) cout<<" Empty:Yes"<< endl;
else cout<<" Empty:No"<< endl;
cout<<" Length="<<ListLength(L)<<",Size="<<L.listsize<< endl;
}
//结构的查改增删
bool GetElem(SqList L,int pos,ElemType & e)
{
if(pos<1||pos>L.length) return false ;
e=L.elem[pos-1 ];
return true ;
}
bool ElemEqual(ElemType e1,ElemType e2)
{
return e1== e2;
}
bool ElemPlus(ElemType e1,ElemType e2)
{
return e1> e2;
}
bool ElemMinus(ElemType e1,ElemType e2)
{
return e1< e2;
}
int LocateElem(SqList L,ElemType e,bool (* compare)(ElemType,ElemType))
{
for(int i=1;i<=L.length;i++ )
if((*compare)(L.elem[i-1],e)) return i;
return 0 ;
}
bool PutElem(SqList &L,int pos,ElemType e)
{
if(pos<1||pos>L.length) return false ;
L.elem[pos-1]= e;
return true ;
}
bool ListInsert(SqList &L,int pos,ElemType e)
{
if(pos<1||pos>L.length+1) return false ;
if(L.length>= L.listsize)
{
ElemType *newbase=new ElemType[L.listsize+ INCREMENT];
if(!newbase) exit(1 );
for(int i=0;i<L.length;i++) newbase[i]= L.elem[i];
delete [] L.elem;
L.elem= newbase;
L.listsize+= INCREMENT;
}
for(int i=L.length-1;i>=pos-1;i-- )
L.elem[i+1]= L.elem[i];
L.elem[pos-1]= e;
L.length++ ;
return true ;
}
bool ListDelete(SqList &L,int pos,ElemType & e)
{
if(pos<1||pos>L.length) return false ;
for(int i=pos;i<L.length;i++ )
L.elem[i-1]= L.elem[i];
e=L.elem[pos-1 ];
L.length-- ;
return true ;
}
//结构的排序操作
void ListSort(SqList & L)
{
for(int i=0;i<L.length-1;i++ )
for(int j=0;j<L.length-1-i;j++ )
if(L.elem[j]>L.elem[j+1 ])
{
int tmp= L.elem[j];
L.elem[j]=L.elem[j+1 ];
L.elem[j+1]= tmp;
}
}
void MergeList(SqList La,SqList Lb,SqList & Lc)
{
ElemType *pa,*pb,*pc,*pa_last,* pb_last;
pa = La.elem; pb = Lb.elem;
Lc.listsize = Lc.length = La.length+ Lb.length;
pc = Lc.elem = new ElemType[Lc.listsize];
if (!Lc.elem) exit(1);
pa_last = La.elem+La.length-1 ;
pb_last = Lb.elem+Lb.length-1 ;
while(pa<=pa_last&&pb<=pb_last){
if (*pa<=*pb) *pc++=*pa++ ;
else *pc++=*pb++ ;
}
while (pa<=pa_last) *pc++=*pa++;
while (pb<=pb_last) *pc++=*pb++;
}
//结构的功能测试
int main()
{
cout<<"顺序表结构的测试:"<< endl;
SqList A;
cout<<"***************************************"<< endl;
cout<<"生成操作:InitList(A)"<< endl;
InitList(A);
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"插入操作:ListInsert(A,1,2)"<< endl;
ListInsert(A,1,2 );
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"插入操作:ListInsert(A,2,7)"<< endl;
ListInsert(A,2,7 );
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"插入操作:ListInsert(A,2,8)"<< endl;
ListInsert(A,2,8 );
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"排序操作:ListSort(A)"<< endl;
ListSort(A);
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"插入操作:ListInsert(A,2,21)"<< endl;
ListInsert(A,2,21 );
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"插入操作:ListInsert(A,4,15)"<< endl;
ListInsert(A,4,15 );
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"删除操作:ListDelete(A,5,e)"<< endl;
ElemType e;
ListDelete(A,5 ,e);
ListInfo(A);
cout<<" e="<<e<< endl;
cout<<"***************************************"<< endl;
cout<<"排序操作:ListSort(A)"<< endl;
ListSort(A);
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"更新操作:PutElem(A,4,39)"<< endl;
PutElem(A,4,39 );
ListInfo(A);
cout<<"***************************************"<< endl;
cout<<"查找操作:GetElem(A,2,e)"<< endl;
GetElem(A,2 ,e);
ListInfo(A);
cout<<" e="<<e<< endl;
cout<<"***************************************"<< endl;
cout<<"定位操作:LocateElem(A,15,ElemEqual)"<< endl;
int Pos=LocateElem(A,15 ,ElemEqual);
ListInfo(A);
cout<<" Pos="<<Pos<< endl;
cout<<"***************************************"<< endl;
cout<<"定位操作:LocateElem(A,6,ElemPlus)"<< endl;
Pos=LocateElem(A,6 ,ElemPlus);
ListInfo(A);
cout<<" Pos="<<Pos<< endl;
cout<<"***************************************"<< endl;
cout<<"定位操作:LocateElem(A,8,ElemMinus)"<< endl;
Pos=LocateElem(A,8 ,ElemMinus);
ListInfo(A);
cout<<" Pos="<<Pos<< endl;
cout<<"***************************************"<< endl;
SqList B;
InitList(B);
ListInsert(B,1,9 );
ListInfo(B);
cout<<"***************************************"<< endl;
SqList C;
InitList(C);
MergeList(A,B,C);
ListOutput(A);
ListOutput(B);
ListOutput(C);
cout<<"按任意键,结束..." ;
cin.get ();
return 0 ;
}