实验1顺序表基本操作Word文件下载.docx
《实验1顺序表基本操作Word文件下载.docx》由会员分享,可在线阅读,更多相关《实验1顺序表基本操作Word文件下载.docx(13页珍藏版)》请在冰豆网上搜索。
![实验1顺序表基本操作Word文件下载.docx](https://file1.bdocx.com/fileroot1/2022-11/16/c31a1537-0864-4edb-9f10-34ad6387564f/c31a1537-0864-4edb-9f10-34ad6387564f1.gif)
/*清空顺序表*/
voidListClear(SeqListL)
/*求顺序表长度*/
intListLength(SeqListL)
/*检查顺序表是否为空*/
intListEmpty(SeqListL)
/*检查顺序表是否为满*/
intListFull(SeqListL)
/*遍历顺序表*/
voidListTraverse(SeqListL)
/*从顺序表中查找元素*/
DataTypeListGet(SeqListL,inti)
/*从顺序表中查找与给定元素值相同的元素在顺序表中的位置*/
intListLocate(SeqListL,DataTypex)
/*向顺序表中插入元素*/
voidListInsert(SeqListL,inti,DataTypex)
/*从顺序表中删除元素*/
voidListDelete(SeqListL,inti)
/*求顺序表中元素的前驱*/
DataTypeListPrior(SeqListL,DataTypee)
/*求顺序表中元素的后继*/
DataTypeListNext(SeqListL,DataTypee)
实验2单链表的实验内容和要求完全类似,实验参考程序见参考程序2
#include<
stdio.h>
/*定义ElemType为int类型*/
typedefintElemType;
#defineMAXSIZE100
#defineFALSE0
#defineTRUE1
{ElemTypedata[MAXSIZE];
intlength;
{SeqListL;
L.length=0;
returnL;
}
SeqListListClear(SeqListL)
{L.length=0;
}
{return(L.length);
{if(L.length)return(FALSE);
elsereturn(TRUE);
{if(L.length==MAXSIZE)return(TRUE);
elsereturn(FALSE);
{inti;
if(L.length<
=0)printf("
顺序表为空\n"
);
else{printf("
当前顺序表中的元素为:
\n"
for(i=1;
i<
=L.length;
i++)printf("
%d"
L.data[i-1]);
printf("
ElemTypeListGet(SeqListL,inti)
{ElemTypee;
e=L.data[i-1];
return(e);
intListLocate(SeqListL,ElemTypex)
{inti=0;
while(i<
L.length&
&
L.data[i]!
=x)
i++;
if(i<
L.length)return(i+1);
elsereturn0;
SeqListListInsert(SeqListL,inti,ElemTypex)
{intj;
if(L.length==MAXSIZE)
表满,不能插入\n"
elseif(i<
1||i>
L.length+1)
插入位置不正确\n"
else{for(j=L.length-1;
j>
=i-1;
j--)
L.data[j+1]=L.data[j];
L.data[i-1]=x;
L.length++;
SeqListListDelete(SeqListL,inti)
ElemTypex;
L.length)
删除位置不正确\n"
else{x=L.data[i-1];
for(j=i;
j<
=L.length-1;
j++)
L.data[j-1]=L.data[j];
L.length--;
printf("
%d已被删除\n"
x);
}
ElemTypeSeqListPrior(SeqListL,ElemTypee)
L.length&
L.data[i]!
=e)
if(i==0){printf("
第一个元素没有前驱\n"
return0;
=L.length-1)return(L.data[i-1]);
else{printf("
不存在值为%d的元素\n"
e);
ElemTypeSeqListNext(SeqListL,ElemTypee)
if(i==L.length-1){printf("
最后一个元素没有后继\n"
L.length-1)return(L.data[i+1]);
intscan()
{intd;
请选择所要进行的操作\n"
1.初始化2.清空3.求顺序表长度4.检查顺序表是否为空\n"
5.检查顺序表是否为满6.遍历顺序表7.从顺序表中查找元素\n"
8.从顺序表中查找与给定元素值相同的元素在顺序表中的位置\n"
9.向顺序表中插入元素10.从顺序表中删除元素\n"
11.求元素的前驱12.求元素的后继\n"
其他键退出......\n"
scanf("
%d"
&
d);
return(d);
main()
{intquit=0;
inti,location;
ElemTypee,prior,next;
SeqListL;
第一次操作需选择初始化\n"
while(!
quit)
switch(scan())
{case1:
L=SeqListInit();
break;
case2:
ListClear(L);
case3:
printf("
顺序表的长度为%d\n"
ListLength(L));
case4:
if(ListEmpty(L))printf("
elseprintf("
顺序表不为空\n"
case5:
if(ListFull(L))printf("
顺序表满\n"
顺序表不满\n"
case6:
ListTraverse(L);
break;
case7:
请输入要查找的元素的位置\n"
i);
if(L.length==0)printf("
顺序表已空\n"
=0||i>
L.length)printf("
查找的位置不正确\n"
else
printf("
顺序表中第%d个元素的值为:
%d\n"
i,ListGet(L,i));
break;
case8:
请输入要查找的元素的值\n"
e);
else
{location=ListLocate(L,e);
if(location==0)printf("
顺序表中不存在值为%d的元素\n"
elseprintf("
顺序表中%d的位置是:
e,ListLocate(L,e));
case9:
请输入要插入的元素的位置和其值:
%d%d"
i,&
L=ListInsert(L,i,e);
case10:
请输入要删除的元素的位置:
L=ListDelete(L,i);
case11:
scanf("
prior=SeqListPrior(L,e);
if(prior)printf("
%d的前驱是:
e,prior);
case12:
next=SeqListNext(L,e);
if(next)printf("
%d的后继是:
e,next);
default:
quit=1;
stdlib.h>
#define