顺序表基本操作的实现.docx

上传人:b****1 文档编号:3593067 上传时间:2023-05-06 格式:DOCX 页数:12 大小:32.66KB
下载 相关 举报
顺序表基本操作的实现.docx_第1页
第1页 / 共12页
顺序表基本操作的实现.docx_第2页
第2页 / 共12页
顺序表基本操作的实现.docx_第3页
第3页 / 共12页
顺序表基本操作的实现.docx_第4页
第4页 / 共12页
顺序表基本操作的实现.docx_第5页
第5页 / 共12页
顺序表基本操作的实现.docx_第6页
第6页 / 共12页
顺序表基本操作的实现.docx_第7页
第7页 / 共12页
顺序表基本操作的实现.docx_第8页
第8页 / 共12页
顺序表基本操作的实现.docx_第9页
第9页 / 共12页
顺序表基本操作的实现.docx_第10页
第10页 / 共12页
顺序表基本操作的实现.docx_第11页
第11页 / 共12页
顺序表基本操作的实现.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

顺序表基本操作的实现.docx

《顺序表基本操作的实现.docx》由会员分享,可在线阅读,更多相关《顺序表基本操作的实现.docx(12页珍藏版)》请在冰点文库上搜索。

顺序表基本操作的实现.docx

顺序表基本操作的实现

1、顺序表根本操作的实现

[问题描述]在顺序表中查找值为x的元素的位置,在线性表的某个位置插入一个元素,删除线性表某个位置的元素。

[根本要求]要求建立生成顺序表,可以键盘上读取元素,用顺序存储构造实现存储。

[实现提示]要实现根本操作,可用实现的根本操作,也可设计简单的算法实现。

[建议步骤]

1〕建立顺序表的存储构造;

2〕利用1〕的存储构造建立有实际数据的数据表;

3〕实现查找操作;

4〕实现插入操作;

5〕实现删除操作。

6〕写出main函数测试上述操作。

实验源码:

#include

#defineMAX300

typedefintElemType;

typedefstruct

{

ElemTypedata[MAX];

intlength;

}SqList;

SqListL;

//打印菜单

voidmenu()

{

printf("**************************************\n");

printf("顺序表操作的验证实验\n");

printf("**************************************\n");

printf("1、初始化表\n");

printf("2、创立表\n");

printf("3、按值查询\n");

printf("4、在指定位置插入一个元素\n");

printf("5、删除指定位置上的一个元素\n");

printf("6、输出表\n");

printf("0、退出\n");

printf("***************************************\n");

}

//初始化表,置表长为0

voidInit(SqList*L)

{

L->length=0;

}

//创立表

voidCreat(SqList*L)

{

intn,i;

printf("请确定表的长度:

");

scanf("%d",&n);

L->length=n;

printf("请输入数据元素:

\n");

for(i=1;i<=n;i++)

scanf("%d",&L->data[i]);

printf("\n\n按任意任意键继续...\n");

getch();

}

//显示列表

voidPrintL(SqList*L)

{

inti;

if(L->length==0)

printf("空表,请首先创立表!

\n");

else

{

printf("\n当前表元素是:

\n");

for(i=1;i<=L->length;i++)

{

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

if(i%10==0)printf("\n");

}

printf("\n");

}

printf("\n\n按任意任意键继续...\n");

getch();

}

//按值查询

voidSearch(SqList*L)

{

inti,x;

printf("请输入要查询元素的值:

");

scanf("%d",&x);

for(i=1;i<=L->length&&L->data[i]!

=x;i++);

if(i<=L->length)

printf("\n元素%d第一次出现在表中第%d个位置上!

\n",x,i);

elseprintf("\n表中没有元素%d!

\n",x);

printf("\n");

printf("\n\n按任意任意键继续...\n");

getch();

}

//在指定位置上插入一个元素

voidInsert(SqList*L)

{

inti,j,x;

printf("请确定要插入的位置:

");

scanf("%d",&i);

printf("请输入要插入的元素值:

");

scanf("%d",&x);

if(L->length>MAX)

{

printf("表满!

\n");

return;

}

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

printf("位置错!

\n");

else

{

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

L->data[j+1]=L->data[j];

L->data[i]=x;

L->length++;

}

PrintL(L);

printf("\n\n插入成功!

按任意任意键继续...\n");

getch();

}

//删除指定位置上的一个元素

voidDel(SqList*L)

{

inti,j;

printf("请确定要删除元素的位置:

");

scanf("%d",&i);

if(L->length==0)

printf("空表!

\n");

else

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

printf("位置错!

\n");

else

{

for(j=i+1;j<=L->length;j++)

L->data[j-1]=L->data[j];

L->length--;

}

PrintL(L);

printf("\n\n删除成功!

按任意任意键继续...\n");

getch();

}

main()

{

intt;

while

(1)

{

system("cls");

menu();

printf("请选择一个操作:

");

scanf("%d",&t);

switch(t)

{

case1:

Init(&L);

break;

case2:

Creat(&L);

break;

case3:

Search(&L);

break;

case4:

Insert(&L);

break;

case5:

Del(&L);

break;

case6:

PrintL(&L);

break;

case0:

exit(0);

default:

printf("输入错误!

请按任意键继续...\n");

getchar();

}

}

}

运行截图:

2、有序顺序表的合并

[问题描述]顺序表la和lb中的数据元素按非递减有序排列,将la和lb表中的数据元素,合并成为一个新的顺序表lc。

[根本要求]lc中的数据元素仍按非递减有序排列,并且不破坏la和lb表。

实验源码:

#include

#defineMAX200

typedefintElemType;

typedefstruct

{

ElemTypedata[MAX];

intlength;

}SqList;

SqListLa,Lb,Lc;

//初始化表

voidInit_List(SqList*L)

{

L->length=0;

}

//创立表

voidCreat_List(SqList*L)

{

intn,i;

printf("请确定表的长度:

");

scanf("%d",&n);

L->length=n;

printf("请输入数据元素:

\n");

for(i=1;i<=n;i++)

scanf("%d",&L->data[i]);

}

//输出表

voidPrint_List(SqList*L)

{

inti;

if(L->length==0)

printf("空表,请首先创立表!

\n");

else

{

printf("\n当前表元素是:

\n");

for(i=1;i<=L->length;i++)

{

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

if(i%10==0)printf("\n");

}

printf("\n");

}

}

//从表中取值

voidGetElem(SqList*L,inti,ElemType*e)

{

if(i>=1&&i<=L->length)

*e=L->data[i];

}

//插入:

voidInsert_List(SqList*L,inti,ElemTypee)

{

intj;

if(L->length>MAX)

{

printf("表满!

\n");

return;

}

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

printf("位置错!

\n");

else

{

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

L->data[j+1]=L->data[j];

L->data[i]=e;

++L->length;

}

}

//合成新表

voidMergeList()

{

inti,j;

ElemTypeai,bj;

intk=0;

i=j=1;

while((i<=La.length)&&(j<=Lb.length))

{//La和Lb均非空

GetElem(&La,i,&ai);

GetElem(&Lb,j,&bj);

if(ai<=bj)

{

Insert_List(&Lc,++k,ai);

++i;

}

else

{

Insert_List(&Lc,++k,bj);

++j;

}

}

while(i<=La.length)

{

GetElem(&La,i++,&ai);

Insert_List(&Lc,++k,ai);

}

while(j<=Lb.length)

{

GetElem(&Lb,j++,&bj);

Insert_List(&Lc,++k,bj);

}

Lc.length=La.length+Lb.length;

}//merge_list

main()

{

//初始化表

Init_List(&La);

Init_List(&Lb);

Init_List(&Lc);

//创立一个测试表

Creat_List(&La);

Creat_List(&Lb);

Print_List(&La);

Print_List(&Lb);

MergeList();

Print_List(&Lc);

}

运行截图:

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2