当前位置:文档之家› 数据结构实验五

数据结构实验五

{ቤተ መጻሕፍቲ ባይዱ
int c,p;
c=0;
if(SearchHash(H,e.key,p,c))
return DUPLICATE;
else if(c<hashsize[H.sizeindex]/2)
{
H.elem[p]=e;
++H.count;
return OK;
}
else
{
RecreateHashTable(H);
ST.elem[i] = ST.elem[0];
}
}
}
void Creat_Ord(SSTable &ST, ElemType *r, int n)
{
Creat_Seq(ST, r, n);
Ascend(ST);
}
int Destory(SSTable &ST)
{
delete []ST.elem;
ST.elem = NULL;
struct ElemType
{
KeyType key;
int ord;
};
int hashsize[]={11,19,29,37};
int m=0;
struct HashTable
{
ElemType *elem;
int count;
int sizeindex;
};
void InitHashTable(HashTable &H)
#define LT(a,b) ((a)<(b))
#define LQ(a,b) ((a)<=(b))
#define N 11
#define SUCCESS 1
#define UNSUCCESS 0
#define DUPLICATE -1
#define NULL_KEY 0
typedef int KeyType;
{
p=Hash(K);
while(H.elem[p].key!=NULL_KEY&&!EQ(K,H.elem[p].key))
{
c++;
if(c<m)
collision(p,c);
else
break;
}
if EQ(K,H.elem[p].key)
return SUCCESS;
else
return UNSUCCESS;
}
void Traverse(SSTable ST, void(*Visit)(ElemType))
{
ElemType *p;
int i;
p = ++ST.elem;
for (i = 1; i <= ST.length; ++i)
Visit(*p++);
}
int main()
{
ElemType r[N] = { { 179328, "何芳芳", 85, 89, 98, 100, 93, 80, 47 },
}
if EQ(K,H.elem[p].key)
return SUCCESS;
else
return UNSUCCESS;
if (!ST.elem)
exit(0);
for (i = 1, j = 0; i <= n; ++i, ++j)
{
ST.elem[i].number = r[j].number;
strcpy(ST.elem[i].name, r[j].name);
ST.elem[i].politics = r[j].politics;
else
printf("没找到\n");
Destory(st);
return 0;
}
(2)程序运行结果:
(3)结果分析:
程序运行正确
任务二:完成下列程序,该程序完成哈希表的开放定址法。在输出结果中显示查找成功与查找不成功信息。
(1)源代码如下:
#include<iostream>
using namespace std;
ST.elem[0] = ST.elem[k];
for (j = i + 1; j <= ST.length; ++j)
{
if (LT(ST.elem[j].key, ST.elem[0].key))
{
k = j;
ST.elem[0] = ST.elem[j];
}
}
if (k != i)
{
ST.elem[k] = ST.elem[i];
实验
实验课程名:数据结构与算法
专业班级:15软件工程1班学号:201540550119姓名:李志强
实验时间:3.17-3.24实验地点:K4-207指导教师:祁文青
一、实验目的和要求
1、掌握查找的不同方法,并能用高级语言实现查找算法。
2、熟练掌握顺序表的查找方法和有序顺序表的折半查找算法。
3、掌握常用的排序方法,并能用高级语言实现排序算法。
if(!p)
exit(OVERFLOW);
H.elem=p;
for(i=0;i<m;i++)
H.elem[i].key=NULL_KEY;
for(p=elem;p<elem+count;p++)
InsertHash(H,*p);
}
Status InsertHash(HashTable &H,ElemType e)
{ 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 } };
}
Status Find(HashTable H,KeyType K,int &p)
{
int c=0;
p=Hash(K);
while(H.elem[p].key!=NULL_KEY&&!EQ(K,H.elem[p].key))
{
c++;
if(c<m)
collision(p,c);
else
return UNSUCCESS;
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;
#define N 5
#define EQ(a, b) ((a) == (b))
#define LT(a, b) ((a) < (b))
#define LQ(a, b) ((a) <= (b))
语文
外语
数学
物理
化学
生物
179328
何芳芳
85
89
98
100
93
80
47
592
179325
陈红
85
86
88
100
92
90
45
586
179326
陆华
78
75
90
80
95
88
37
543
179327
张平
82
80
78
98
84
96
40
558
179324
赵小怡
76
85
94
57
77
69
44
502
(1)源代码如下:
}
void Visit(ElemType c)
{
printf("%-8ld%-8s%4d%5d%5d%5d%5d%5d%5d%5d\n", c.number, , c.politics,c.Chinese, c.English, c.Math, c.Physics, c.Chemistry, c.Biology, c.total);
int Biology;
int total;
}ElemType;
typedef struct
{
ElemType *elem;
int length;
}SSTable;
void Creat_Seq(SSTable &ST, ElemType *r, int n)
{
int i, j;
ST.elem = new ElemType[n + 1];
return UNSUCCESS;
}
}
void TraverseHash(HashTable H,void(*Vi)(int,ElemType))
{
printf("哈希地址0~%d\n",m-1);
for(int i=0;i<m;i++)
if(H.elem[i].key!=NULL_KEY)
Vi(i,H.elem[i]);
ST.elem[i].Chinese = r[j].Chinese;
ST.elem[i].English = r[j].English;
ST.elem[i].Math = r[j].Math;
相关主题