线性表的数组表示和实现.docx
《线性表的数组表示和实现.docx》由会员分享,可在线阅读,更多相关《线性表的数组表示和实现.docx(18页珍藏版)》请在冰豆网上搜索。
![线性表的数组表示和实现.docx](https://file1.bdocx.com/fileroot1/2023-2/3/f1b70a79-a5ec-46cd-ae1c-9eb1862d2efd/f1b70a79-a5ec-46cd-ae1c-9eb1862d2efd1.gif)
线性表的数组表示和实现
线性表的数组表示和实现
程序构思:
本程序将elem表示成数据类型为结构体ElemType的数组。
首先给出了线性表的类定义,然后给出了线性表中函数原型的实现部分,最后对线性表中的主要操作进行了测试,输出运行结果。
其中,对线性表按升序或降序输出printlist(intmark)函数的形参mark为标识符,mark=0无序,mark=1升序,mark=-1降序。
注意:
本段代码全部在vc++6.0上运行通过。
linelist1.h
#defineMaxListSize20
#defineEQUAL1
typedefstructSTU{
charname[10];
charstuno[10];
intage;
floatscore;
}ElemType;
classList
{
private:
ElemTypeelem[MaxListSize];
intlength;
intMaxSize;
public:
//初始化顺序表
voidinit(List**L,intms);
//删除顺序表
voidDestroyList(List&L);
//将顺序表置为空表
voidClearList();
//判断顺序表是否为空
boolListEmpty();
//判断顺序表是否为满
boolListFull();
//决定返回表中元素pre_e的前驱
ElemTypePriorElem(ElemTypecur_e,ElemType&pre_e);
//决定返回表中元素next_e的后继
ElemTypeList:
:
NextElem(ElemTypecur_e,ElemType&next_e);
//从线性表中删除表头,表尾,或等于给定值的元素
boolListDelete(int,ElemType&e);
//遍历顺序表
voidListTraverse();
//返回顺序表的长度
intListLength();
//获取顺序表中的第i个元素
voidGetElem(int,ElemType*);
//判断顺序表两元素是否相等
boolEqualList(ElemType*,ElemType*);
//判断顺序表两元素是否不等
boolLess_EqualList(ElemType*,ElemType*);
//顺序表的查找算法
boolLocateElem(ElemType,int);
//更新线性表中的给定元素
boolUpdateList(ElemType&e,ElemType);
//顺序表的合并算法
voidMergeList(List*,List*);
//顺序表的插入算法
boolListInsert(int,ElemType&);
//顺序表的联合算法
voidUnionList(List*,List*);
//对线性表按升序或降序输出
voidprintlist(int);
};
linelist1.cpp
#include"linelist1.h"
#include
#include
#include
#include
//初始化顺序表
voidList:
:
init(List**L,intms)
{
(*L)=(List*)malloc(sizeof(List));
(*L)->length=0;
(*L)->MaxSize=ms;
}
//删除顺序表
voidList:
:
DestroyList(List&L)
{
free(&L);
}
//将顺序表置为空表
voidList:
:
ClearList()
{
length=0;
}
//判断顺序表是否为空
boolList:
:
ListEmpty()
{
if(length==0)
returntrue;
else
returnfalse;
}
//判断顺序表是否为满
boolList:
:
ListFull()
{
if(length==MaxSize)
returntrue;
else
returnfalse;
}
//决定返回表中元素pre_e的前驱
ElemTypeList:
:
PriorElem(ElemTypecur_e,ElemType&pre_e)
{
for(inti=0;iif((i!
=0)&&strcmp(cur_e.name,elem[i].name)==0)
{
pre_e=elem[i-1];
returnpre_e;
}
returncur_e;
}
//决定返回表中元素next_e的后继
ElemTypeList:
:
NextElem(ElemTypecur_e,ElemType&next_e)
{
for(inti=0;iif((i!
=length-1)&&strcmp(cur_e.name,elem[i].name)==0)
{
next_e=elem[i+1];
returnnext_e;
}
returncur_e;
}
//从线性表中删除表头,表尾,或等于给定值的元素
boolList:
:
ListDelete(intmark,ElemType&e)//mark>0删除表头mark<0删除表尾mark=0删除e
{
inti,j;
if(ListEmpty())
returnfalse;
if(mark>0)
{
e=elem[0];
for(i=1;ielem[i-1]=elem[i];
}
else
if(mark<0)
e=elem[length-1];
else{
for(i=0;iif(strcmp(elem[i].name,e.name))
break;
if(i>=length)
returnfalse;
else
e=elem[i];
for(j=i+1;jelem[i-1]=elem[i];}
length--;
returntrue;
}
//遍历顺序表
voidList:
:
ListTraverse()
{
for(inti=0;i{
cout<cout<cout<cout<}
}
//返回顺序表的长度
intList:
:
ListLength()
{
returnlength;
}
//获取顺序表中的第i个元素
voidList:
:
GetElem(inti,ElemType*e)
{
while(i<1||i>length)
{
cout<<"请输入1-"<";
cin>>i;
}
*e=elem[i-1];
}
//判断顺序表两元素是否相等
boolList:
:
EqualList(ElemType*e1,ElemType*e2)
{
if(strcmp((*e1).name,(*e2).name))
returnfalse;
if((*e1).age!
=(*e2).age)
returnfalse;
if(strcmp((*e1).stuno,(*e2).stuno))
returnfalse;
if((*e1).score!
=(*e2).score)
returnfalse;
returntrue;
}
//判断顺序表两元素是否不等
boolList:
:
Less_EqualList(ElemType*e1,ElemType*e2)
{
if(strcmp(e1->name,e2->name)==0)
returnfalse;
else
returntrue;
}
//顺序表的查找算法
boolList:
:
LocateElem(ElemTypee,inttype)
{
inti;
switch(type){
caseEQUAL:
for(i=0;iif(EqualList(&elem[i],&e))
returntrue;
break;
default:
break;}
returnfalse;
}
//更新线性表中的给定元素
boolList:
:
UpdateList(ElemType&e,ElemTypee1)
{
for(inti=0;iif(strcmp(elem[i].name,e.name)==0)
{
elem[i]=e1;
returntrue;
}
returnfalse;
}
//顺序表的合并算法
voidList:
:
MergeList(List*La,List*Lb)
{
intLa_len,Lb_len;
La_len=La->ListLength();
Lb_len=Lb->ListLength();
for(inti=0;ielem[i]=La->elem[i];
for(intj=0;jelem[j+i]=Lb->elem[j];
length=La_len+Lb_len;
//MaxSize=La->MaxSize+Lb->MaxSize;
}
//顺序表的插入算法
boolList:
:
ListInsert(inti,ElemType&e)
{
ElemType*p,*q;
if(i<1||i>length+1)
returnfalse;
q=&elem[i-1];
p=&elem[length-1];
for(p;p>=q;--p)
*(p+1)=*p;
*q=e;
length++;
returntrue;
}
//顺序表的联合算法
voidList:
:
UnionList(List*La,List*Lb)
{
inti,La_len,Lb_len;
ElemTypee;
La_len=La->ListLength();
Lb_len=Lb->ListLength();
for(i=0;i{
Lb->GetElem(i+1,&e);
if(!
LocateElem(e,EQUAL))
ListInsert(++La_len,e);
}
}
//对线性表按升序或降序输出
voidList:
:
printlist(intmark)//mark=0无序mark=1升序mark=-1降序
{
int*b=newint[length];
inti,k;
cout<<"姓名学好年龄成绩\n";
if(mark!
=0)
{
for(i=0;ib[i]=i;
for(i=0;i{
k=i;
for(intj=i+1;j{
if(mark==1&&elem[b[j]].scorek=j;
if(mark==-1&&elem[b[k]].scorek=j;
}
if(k!
=i)
{
intx=b[i];
b[i]=b[k];
b[k]=x;
}
}
for(i=0;i{
cout<cout<cout<cout<}
}
else
{
for(i=0;i{
cout<cout<cout<cout<}
}
}
Linelist1m.cpp
#include
#include
#include
#include
#include"linelist1.h"
voidmain()
{
cout<<"--------------linelist----------------"<ElemTypee,e1,e2,e3,e4,e5,e6;
List*La,*Lb,*Lc;
intk;
La->init(&La,4);
strcpy(e1.name,"stu1");
strcpy(e1.stuno,"100001");
e1.age=22;
e1.score=88;
La->ListInsert(1,e1);
strcpy(e2.name,"stu2");
strcpy(e2.stuno,"100002");
e2.age=20;
e2.score=95;
La->ListInsert(2,e2);
strcpy(e3.name,"stu3");
strcpy(e3.stuno,"100003");
e3.age=20;
e3.score=85;
La->ListInsert(3,e3);
La->printlist(0);
cout<<"表La的长度为:
"<ListLength()<cin.get();
Lb->init(&Lb,4);
strcpy(e4.name,"zmofun");
strcpy(e4.stuno,"100001");
e4.age=20;
e4.score=94;
Lb->ListInsert(1,e4);
strcpy(e5.name,"bobjin");
strcpy(e5.stuno,"100002");
e5.age=23;
e5.score=69;
Lb->ListInsert(2,e5);
strcpy(e6.name,"stu1");
strcpy(e6.stuno,"100001");
e6.age=22;
e6.score=88;
Lb->ListInsert(3,e6);
Lb->printlist(0);
cout<<"表Lb的长度为:
"<ListLength()<cin.get();
cout<<"表La与Lb合并的表Lc为:
"<Lc->init(&Lc,6);
Lc->MergeList(La,Lb);
Lc->printlist(0);
cout<<"合并后的表长为:
"<ListLength()<cin.get();
cout<<"表La与Lb联合为La:
"<La->UnionList(La,Lb);
La->printlist(0);
cout<<"合并后的表长为:
"<ListLength()<cin.get();
k=Lc->ListDelete(-1,e6);
if(k==0)
cout<<"删除失败!
"<else
cout<<"删除成功!
"<cout<<"输出表Lc:
"<Lc->printlist(0);
cin.get();
strcpy(e.name,"NoName");
La->PriorElem(e2,e);
if(strcmp(e.name,"NoName")==0)
cout<<"e2无前驱"<else
cout<<"e2的前驱e.name="<strcpy(e.name,"NoName");
La->NextElem(e3,e);
if(strcmp(e.name,"NoName")==0)
cout<<"e3无前驱"<else
cout<<"e3的前驱e.name="<cin.get();
cout<<"按成绩升序输出表Lc:
"<Lc->printlist
(1);
cin.get();
cout<<"按成绩降序输出表Lc:
"<Lc->printlist(-1);
cin.get();
}
程序输出的结果为: