顺序表的应用.docx
《顺序表的应用.docx》由会员分享,可在线阅读,更多相关《顺序表的应用.docx(9页珍藏版)》请在冰豆网上搜索。
顺序表的应用
实验一顺序表的应用
一、实验目的
1.掌握线性表的顺序存储结构的存储特点与操作特点。
2.掌握顺序表的存储表示与基本操作的实现方法。
3.熟悉顺序表的基本应用。
4.了解抽象数据类型的定义、表示与实现的含义。
二、实验容
按如下要求编写程序,进行调试,写出调试正确的源代码,给出测试结果。
实现线性表在顺序存储结构下的存储表示和基本操作,并应用抽象数据类型线性表完成如下操作:
1.分别创建两个空的线性表L1、L2;
2.分别将若干元素插入到L1、L2中,输出操作后表L1和L2的容;
3.实现两表的合并操作:
L1=L1∪L2,输出合并后表L1的容。
4.实现两表的求差操作:
L1=L1-L2,输出求差后表L1的容。
三、实验结果
源代码:
#include
#include
#defineOK1
#defineTRUE1
#defineFALSE0
#defineERROR0
#defineOVERFLOW-2
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
typedefintStatus;
typedefintElemType;
typedefstruct//存储表示
{
ElemType*elem;
intlength;
intlistsize;
}SqList;
//基本操作的函数声明
StatusInitList_Sq(SqList&L);
StatusListInsert_Sq(SqList&L,inti,ElemTypee);
Statusequal(ElemTypea,ElemTypeb);
StatusGetElem_Sq(SqListL,inti,ElemType&e);
intListLength_Sq(SqListL);
voidUnoin(SqList&La,SqListLb);
voidcha(SqList&La,SqList&Lb);
intLocateElem_Sq(SqListL,ElemTypee,Status(*compare)(ElemType,ElemType));
//基本操作的算法实现
StatusInitList_Sq(SqList&L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!
L.elem)exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
returnOK;
}
StatusListInsert_Sq(SqList&L,inti,ElemTypee)
{
ElemType*newbase,*p,*q;
if(i<1||i>L.length+1)
returnERROR;
if(L.length>=L.listsize)
{
newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!
newbase)exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
returnOK;
}
StatusListDelete_Sq(SqList&L,inti,ElemType&e)
{
ElemType*p,*q;
if((i<1)||(i>L.length))
returnERROR;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--L.length;
returnOK;
}
voidcha(SqList&La,SqList&Lb)
{
intla_len,lb_len,i,e;
la_len=ListLength_Sq(La);
lb_len=ListLength_Sq(Lb);
for(i=1;i<=lb_len;i++)
{
GetElem_Sq(Lb,i,e);
if(LocateElem_Sq(La,e,equal))
ListDelete_Sq(La,LocateElem_Sq(La,e,equal),e);
}
}
Statusequal(ElemTypea,ElemTypeb)
{
if(a==b)
returnTRUE;
elsereturnFALSE;
}
StatusGetElem_Sq(SqListL,inti,ElemType&e)
{
if(i<1||i>L.length)
returnERROR;
e=*(L.elem+i-1);
returnOK;
}
intListLength_Sq(SqListL)
{
returnL.length;
}
voidUnoin(SqList&La,SqListLb)
{
intla_len,lb_len,i,e;
la_len=ListLength_Sq(La);
lb_len=ListLength_Sq(Lb);
for(i=1;i<=lb_len;i++)
{
GetElem_Sq(Lb,i,e);
if(!
LocateElem_Sq(La,e,equal))
ListInsert_Sq(La,++la_len,e);
}
}
intLocateElem_Sq(SqListL,ElemTypee,Status(*compare)(ElemType,ElemType))
{
inti=1;ElemType*p;
p=L.elem;
while(i<=L.length&&!
(*compare)(*p++,e))++i;
if(i<=L.length)returni;
elsereturn0;
}
voidmain()
{
intm,n,i;
ElemTypee;
SqListL1,L2;
InitList_Sq(L1);
InitList_Sq(L2);
cout<<"请输入L1表的长度";
cin>>m;
cout<<"请输入L2表的长度";
cin>>n;
cout<<"请输入"<for(i=1;i<=m;i++)
{
cin>>e;
ListInsert_Sq(L1,i,e);
}
cout<<"请输入"<for(i=1;i<=n;i++)
{
cin>>e;
ListInsert_Sq(L2,i,e);
}
cout<<"L1表的容"<for(i=1;i<=ListLength_Sq(L1);i++)
{
GetElem_Sq(L1,i,e);
cout<}
cout<cout<<"L2表的容"<for(i=1;i<=ListLength_Sq(L2);i++)
{
GetElem_Sq(L2,i,e);
cout<}
cout<cout<<"输出L1和L2的合并后";
Unoin(L1,L2);
cout<<"L1表的容"<for(i=1;i<=ListLength_Sq(L1);i++)
{
GetElem_Sq(L1,i,e);
cout<}
cout<cout<<"输出L1和L2的差后";
cha(L1,L2);
cout<<"L1表的容"<for(i=1;i<=ListLength_Sq(L1);i++)
{
GetElem_Sq(L1,i,e);
cout<}
cout<}
运行结果:
1
运行结果:
2