《线性表的顺序存储》实验报告

  • 格式:docx
  • 大小:79.96 KB
  • 文档页数:6

下载文档原格式

  / 6
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《线性表的顺序存储》实验报告1.需解决的的问题

利用顺序表,设计一组输入数据。

2.数据结构的定义

typedefstruct{

ElemType *elem;

int length;

intlistsize;

}SqList;

3.程序的结构图

4.函数的功能

1)初始化一个空顺序表

voidInitSqList(SqList *L){

L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));

if(!L->elem) exit(OVERFLOW);

L->length=0;

L->listsize=LIST_INIT_SIZE;

}

2)输入元素

voidPushSqList(SqList *L){

inti;

printf("input the length of the list:");

scanf("%d",&L->length);

printf("input the sqlist:");

for(i=0;ilength;i++){

printf("input the %dth number:",i+1);

scanf("%d",&L->elem);

}

}

3)在指定位置插入一个指定的元素

voidInsertSqList(SqList *L,inti,ElemType x){

ElemType *newbase;

intn,m;

if(i<1||i>L->length+1){

printf("ERROR!");

}

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;

}

else{

for(n=L->length;n>=i;n--)

{

++L->length;

L->elem[n]=L->elem[n-1];

}

L->elem[i-1]=x;

printf("the list is:");

for(m=0;mlength+1;n++)

printf("%d",L->elem[n]);

}

}

4)删除指定位置的元素

voidDelSqList(SqList *L,inti){

ElemType *p,*q;

ElemType x;

int n;

if(i<1||i>L->length)

printf("ERROR!");

p=&(L->elem[i-1]);

x=*p;

for(q=p;q<&(L->elem[L->length-1]);q++)

*q=*(q+1);

L->length--;

printf("the element which is delete is %d",x);

printf("the list is:");

for(n=0;nlength-1;n++)

printf("%d",L->elem[n]);

}

5)将顺序表中所有的元素颠倒

voidChangeoverSqList(SqList *L){

SqList S;

inti,j;

if(L->length==L->listsize)

S.elem=(ElemType*)malloc(L->length*sizeof(ElemType));

if(!S.elem) exit(OVERFLOW);

else{

for(i=0;ilength;i++)

S.elem[i]=L->elem[L->length-i-1];

for(i=0;ilength;i++)

L->elem[i]=S.elem[i];

}

printf("the list is:");

for(j=0;jlength;i++)

printf("%d",L->elem[i]);

}

6)按顺序输出表中元素

voidPrintSqList(SqList *L){

inti;

for(i=0;ilength;i++)

printf("%d ",L->elem[i]);

}

7)摧毁顺序表

voidDestroySqList(SqList *L)

{

if(L->elem) free(L->elem);

}

8)清空顺序表

voidClearSqList(SqList *L){

L->length=0;

}

9)查找指定位置的元素,并返回该元素的值intGetElem(SqList *L,inti){

ElemType x;

if(i<1||i>L->length+1) {

printf("ERROR!");

}

else{

x=L->elem[i-1];

printf("the elem is %d",x);

}

}

5.输入/输出数据

1)创建一个顺序表,先输入表长度,然后输入数据

2)选择菜单,进行不同操作