顺序表基本实验内容完整可运行程序.docx
《顺序表基本实验内容完整可运行程序.docx》由会员分享,可在线阅读,更多相关《顺序表基本实验内容完整可运行程序.docx(19页珍藏版)》请在冰豆网上搜索。
顺序表基本实验内容完整可运行程序
/*pubuse.h*/
#include
#include
#include/*malloc()等*/
#include/*INT_MAX等*/
#include/*EOF(=^Z或F6),NULL*/
#include/*atoi()*/
#include/*eof()*/
#include/*floor(),ceil(),abs()*/
#include/*exit()*/
/*函数结果状态代码*/
#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineINFEASIBLE-1
/*#defineOVERFLOW-2因为在math.h中已定义OVERFLOW的值为3,故去掉此行*/
typedefintStatus;/*Status是函数的类型,其值是函数结果状态代码,如OK等*/
typedefintBoolean;/*Boolean是布尔类型,其值是TRUE或FALSE*/
/*Def.h*/
#defineLIST_INIT_SIZE10/*线性表存储空间的初始分配量*/
#defineLISTINCREMENT2/*线性表存储空间的分配增量*/
//typedefintElemType;
typedefstruct
{
ElemType*elem;/*存储空间基址*/
intlength;/*当前长度*/
intlistsize;/*当前分配的存储容量(以sizeof(ElemType)为单位)*/
}SqList;
/*Algo.h*/
StatusInitList(SqList&L)/*算法2.3*/
{/*操作结果:
构造一个空的顺序线性表*/
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!
L.elem)
exit(OVERFLOW);/*存储分配失败*/
L.length=0;/*空表长度为0*/
L.listsize=LIST_INIT_SIZE;/*初始存储容量*/
returnOK;
}
StatusDestroyList(SqList&L)
{/*初始条件:
顺序线性表L已存在。
操作结果:
销毁顺序线性表L*/
free(L.elem);
L.elem=NULL;
L.length=0;
L.listsize=0;
returnOK;
}
StatusClearList(SqList&L)
{/*初始条件:
顺序线性表L已存在。
操作结果:
将L重置为空表*/
L.length=0;
returnOK;
}
StatusListEmpty(SqListL)
{/*初始条件:
顺序线性表L已存在。
操作结果:
若L为空表,则返回TRUE,否则返回FALSE*/
if(L.length==0)
returnTRUE;
else
returnFALSE;
}
intListLength(SqListL)
{/*初始条件:
顺序线性表L已存在。
操作结果:
返回L中数据元素个数*/
returnL.length;
}
StatusGetElem(SqListL,inti,ElemType&e)
{/*初始条件:
顺序线性表L已存在,1≤i≤ListLength(L)*/
/*操作结果:
用e返回L中第i个数据元素的值*/
if(i<1||i>L.length)
exit(ERROR);
e=L.elem[i-1];
returnOK;
}
intLocateElem(SqListL,ElemTypee,Status(*compare)(ElemType,ElemType))
{/*初始条件:
顺序线性表L已存在,compare()是数据元素判定函数(满足为1,否则为0)*/
/*操作结果:
返回L中第1个与e满足关系compare()的数据元素的位序。
*/
/*若这样的数据元素不存在,则返回值为0。
算法2.6*/
ElemType*p;
inti=1;/*i的初值为第1个元素的位序*/
p=L.elem;/*p的初值为第1个元素的存储位置*/
while(i<=L.length&&!
compare(*p++,e))
++i;
if(i<=L.length)
returni;
else
return0;
}
StatusPriorElem(SqListL,ElemTypecur_e,ElemType&pre_e)
{/*初始条件:
顺序线性表L已存在*/
/*操作结果:
若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,*/
/*否则操作失败,pre_e无定义*/
inti=2;
ElemType*p=L.elem+1;
/*ElemType*p=L.elem;
if(*p==cur_e)returnERROR;
elsep++;*///屏蔽的文字用于防止前两个元素相等时,cur_e取该值
while(i<=L.length&&*p!
=cur_e)
{
p++;
i++;
}
if(i>L.length)
returnINFEASIBLE;
else
{
pre_e=*--p;
returnOK;
}
}
StatusNextElem(SqListL,ElemTypecur_e,ElemType&next_e)
{/*初始条件:
顺序线性表L已存在*/
/*操作结果:
若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,*/
/*否则操作失败,next_e无定义*/
inti=1;
ElemType*p=L.elem;
while(i=cur_e)
{
i++;
p++;
}
if(i==L.length)
returnINFEASIBLE;
else
{
next_e=*++p;
returnOK;
}
}
StatusListInsert(SqList&L,inti,ElemTypee)/*算法2.4*/
{/*初始条件:
顺序线性表L已存在,1≤i≤ListLength(L)+1*/
/*操作结果:
在L中第i个位置之前插入新的数据元素e,L的长度加1*/
ElemType*newbase,*q,*p;
if(i<1||i>L.length+1)/*i值不合法*/
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;/*q为插入位置*/
for(p=L.elem+L.length-1;p>=q;--p)/*插入位置及之后的元素右移*/
*(p+1)=*p;
*q=e;/*插入e*/
++L.length;/*表长增1*/
returnOK;
}
StatusListDelete(SqList&L,inti,ElemType&e)/*算法2.5*/
{/*初始条件:
顺序线性表L已存在,1≤i≤ListLength(L)*/
/*操作结果:
删除L的第i个数据元素,并用e返回其值,L的长度减1*/
ElemType*p,*q;
if(i<1||i>L.length)/*i值不合法*/
returnERROR;
p=L.elem+i-1;/*p为被删除元素的位置*/
e=*p;/*被删除元素的值赋给e*/
q=L.elem+L.length-1;/*表尾元素的位置*/
for(++p;p<=q;++p)/*被删除元素之后的元素左移*/
*(p-1)=*p;
L.length--;/*表长减1*/
returnOK;
}
StatusListTraverse(SqListL,void(*vi)(ElemType&))
{/*初始条件:
顺序线性表L已存在*/
/*操作结果:
依次对L的每个数据元素调用函数vi()。
一旦vi()失败,则操作失败*/
/*vi()的形参加'&',表明可通过调用vi()改变元素的值*/
ElemType*p;
inti;
p=L.elem;
for(i=1;i<=L.length;i++)
vi(*p++);
printf("\n");
returnOK;
}
/*Use.cpp*/
/*main2-1.c检验bo2-1.c的主程序*/
#include"pubuse.h"
typedefintElemType;
#include"def.h"
#include"algo.h"
Statuscomp(ElemTypec1,ElemTypec2)/*数据元素判定函数(平方关系)*/
{
if(c1==c2*c2)
returnTRUE;
else
returnFALSE;
}
voidvisit(ElemType&c)/*ListTraverse()调用的函数(类型要一致)*/
{
printf("%d",c);
}
voiddbl(ElemType&c)/*ListTraverse()调用的另一函数(元素值加倍)*/
{
c*=2;
}
voidmain()
{
SqListL;
ElemTypee,e0;
Statusi;
intj,k;
i=InitList(L);
printf("初始化L后:
L的首地址L.elem=%uL.length=%dL.listsize=%d\n",L.elem,L.length,L.listsize);
for(j=1;j<=5;j++)
i=ListInsert(L,1,j);
printf("在L的表头依次插入1~5后:
L=");
for(j=1;j<=5;j++)
printf("%d",L.elem[j-1]);
printf("\n");
printf("L.elem=%uL.