实验五查找及排序讲解Word格式文档下载.docx

上传人:b****2 文档编号:14976593 上传时间:2022-10-26 格式:DOCX 页数:21 大小:76.15KB
下载 相关 举报
实验五查找及排序讲解Word格式文档下载.docx_第1页
第1页 / 共21页
实验五查找及排序讲解Word格式文档下载.docx_第2页
第2页 / 共21页
实验五查找及排序讲解Word格式文档下载.docx_第3页
第3页 / 共21页
实验五查找及排序讲解Word格式文档下载.docx_第4页
第4页 / 共21页
实验五查找及排序讲解Word格式文档下载.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

实验五查找及排序讲解Word格式文档下载.docx

《实验五查找及排序讲解Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《实验五查找及排序讲解Word格式文档下载.docx(21页珍藏版)》请在冰豆网上搜索。

实验五查找及排序讲解Word格式文档下载.docx

98

100

93

80

47

592

179325

陈红

P86

88

92

—90

45

586

179326

陆华

78

75

90

95

37

543

179327

张平

82

84

96

40

558

179324

赵小怡

76

94

57

77

69

44

502

找成功与查找不成功信息。

解答:

(1)源代码:

#include<

stdio.h>

//EOF("

Z或F6),NULL

#include<

stdlib.h>

//atoi()

#include<

io.h>

//eof()

math.h>

//floor(),ceil(),abs()

process.h>

//exit()

iostream.h>

//cout,cin

//函数结果状态代码

#defineTRUE1

#defineFALSE0

#defineOK1

#defineERROR0

#defineINFEASIBLE-1

//#defineOVERFLOW-2因为在math.h中已定义OVERFLOW®

为3,故去掉此行

typedefintStatus;

//Status是函数的类型,其值是函数结果状态代码,

如OK等

typedefintBoolean;

//Boolean是布尔类型,其值是TRUE或FALSE

#defineMAX_LENGTH100

#includevstring.h>

ctype.h>

或F6),NULL

malloc.h>

//malloc()#include<

limits.h>

//INT_MAX#include<

//EOF(=AZ

//atoi()#include<

//floor(),ceil(),abs()#include<

#defineINFEASIBLE-1

//Status

typedefintBoolean;

//Boolean

#defineN5//数据元素个数

#defineEQ(a,b)((a)==(b))

#defineLT(a,b)((a)<

(b))

设关键字域为长整型

定义关键字为准考证号

数据元素类型(以教科书图9.1高考成绩为例)

#defineLQ(a,b)((a)<

=(b))

typedeflongKeyType;

//

#definekeynumber//

structElemType//

准考证号,与关键字类型同

姓名(4个汉字加1个串结束标志)政治

英语

{

longnumber;

charname[9];

intpolitics;

intChinese;

intEnglish;

intmath;

intphysics;

intchemistry;

intbiology;

inttotal;

};

typedefstruct{

ElemType*elem;

//数据元素存储空间基址,建表时按实际长度分配,

号单元留空

intlength;

//表长度

}SSTable;

voidCreat_Seq(SSTable&

ST,ElemTyper[],intn)

{//操作结果:

由含n个数据元素的数组r构造静态顺序查找表ST

inti;

ST.elem=(ElemType*)calloc(n+1,sizeof(ElemType));

//动态生成

个数据元素空间(0号单元不用)

if(!

ST.elem)

exit(ERROR);

for(i=1;

i<

=n;

i++)

ST.elem[i]=r[i-1];

//将数组r的值依次赋给ST

ST.length=n;

}

voidAscend(SSTable&

ST)

{//重建静态查找表为按关键字非降序排序

inti,j,k;

ST.length;

k=i;

ST.elem[O]=ST.elem[i];

//待比较值存[0]单元

for(j=i+1;

j<

=ST」ength;

j++)

ifLT(ST.elem[j].key,ST.elem[O].key)

k=j;

ST.elem[0]=ST.elem[j];

if(k!

=i)//有更小的值则交换

ST.elem[k]=ST.elem[i];

ST.elem[i]=ST.elem[O];

voidCreat_Ord(SSTable&

由含n个数据元素的数组r构造按关键字非降序查找表

Creat_Seq(ST,r,n);

//建立无序的查找表ST

Ascend(ST);

//将无序的查找表ST重建为按关键字非降序查找表

StatusDestroy(SSTable&

{//初始条件:

静态查找表ST存在。

操作结果:

销毁表ST

free(ST.elem);

ST.elem=NULL;

ST.length=O;

n+1

ST

returnOK;

intSearch_Seq(SSTableST,KeyTypekey)

{//在顺序表ST中顺序查找其关键字等于key的数据元素。

若找到,则返回

//该元素在表中的位置,否则返回0。

算法9.1

ST.elem[0].key=key;

//哨兵

for(i=ST.Iength;

!

EQ(ST.elem[i].key,key);

--i);

//从后往前找

returni;

//找不到时,i为0

voidTraverse(SSTableST,void(*Visit)(ElemType))

静态查找表ST存在,Visit()是对元素操作的应用函数

//操作结果:

按顺序对ST的每个元素调用函数Visit()一次且仅一次

ElemType*p;

p=++ST.elem;

//p指向第一个元素

=ST.length;

Visit(*p++);

voidprint(ElemTypec)//Traverse。

调用的函数

printf("

%-8ld%-8s%4d%5d%5d%5d%5d%5d%5d%5d\n"

c.number,c.name

c.politics,

c.Chinese,c.English,c.math,c.physics,c.chemistry,c.biology,c.total);

intmain()

ElemTyper[N]={{179328,"

何芳芳"

85,89,98,100,93,80,47},

{179325,"

陈红"

85,86,88,100,92,90,45},

{179326,"

陆华"

78,75,90,80,95,88,37},

{179327,"

张平"

82,80,78,98,84,96,40},

{179324,"

赵小怡"

76,85,94,57,77,69,44}};

//数组不按关键字有序

SSTablest;

longs;

for(i=0;

N;

i++)//计算总分

r[i].total=r[i].politics+r[i].Chinese+r[i].English+r[i].math

+r[i].physics+

r[i].chemistry+r[i].biology;

CreatSeq(st,r,N);

//由数组r产生顺序静态查找表st

printf("

准考证号姓名政治语文外语数学物理化学生物总分

\n"

);

Traverse(st,print);

//按顺序输出静态查找表st

请输入待查找人的考号:

"

scanf("

%ld"

&

s);

i=Search_Seq(st,s);

//顺序查找

if(i)

print(st.elem[i]);

else

没找到\n"

Destroy(st);

return0;

(2)运行结果:

'

C:

\UsersYJl,?

X\Documerrts\C-Free\Temp'

捽静皂1.exer,

(3)运行结果分析:

运用顺序结构完成查询。

任务二:

哈希表的开放定址法算法。

在输出结果中显示查找成功与查找不成功信息。

(1)源代码:

string.h>

//malloc()等

//INT_MAX等

//EOF(=AZ或F6),NULL

#defineFALSE0

#define

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

当前位置:首页 > 人文社科

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1