算法与数据结构实验五 查找

实验项目名称:实验    查找  

一、 实验目的
1.掌握散列表的建立
2.掌握散列查找算法的实现。

二、 实验内容

6-2 线性探测法的查找函数

试实现线性探测法的查找函数。

函数接口定义:

Position Find( HashTable H, ElementType Key );

其中HashTable是开放地址散列表,定义如下:

#define MAXTABLESIZE 100000  /* 允许开辟的最大散列表长度 */

typedef int ElementType;     /* 关键词类型用整型 */

typedef int Index;           /* 散列地址类型 */

typedef Index Position;      /* 数据所在位置与散列地址是同一类型 */

/* 散列单元状态类型,分别对应:有合法元素、空单元、有已删除元素 */

typedef enum { Legitimate, Empty, Deleted } EntryType;

 

typedef struct HashEntry Cell; /* 散列表单元类型 */

struct HashEntry{

    ElementType Data; /* 存放元素 */

    EntryType Info;   /* 单元状态 */

};

 

typedef struct TblNode *HashTable; /* 散列表类型 */

struct TblNode {   /* 散列表结点定义 */

    int TableSize; /* 表的最大长度 */

    Cell *Cells;   /* 存放散列单元数据的数组 */

};

函数Find应根据裁判定义的散列函数Hash( Key, H->TableSize )从散列表H中查到Key的位置并返回。如果Key不存在,则返回线性探测法找到的第一个空单元的位置;若没有空单元,则返回ERROR。

裁判测试程序样例:

#include <stdio.h>#define MAXTABLESIZE 100000  /* 允许开辟的最大散列表长度 */typedef int ElementType;     /* 关键词类型用整型 */typedef int Index;           /* 散列地址类型 */typedef Index Position;      /* 数据所在位置与散列地址是同一类型 *//* 散列单元状态类型,分别对应:有合法元素、空单元、有已删除元素 */typedef enum { Legitimate, Empty, Deleted } EntryType;typedef struct HashEntry Cell/* 散列表单元类型 */struct HashEntry{    ElementType Data; /* 存放元素 */    EntryType Info;   /* 单元状态 */};typedef struct TblNode *HashTable/* 散列表类型 */struct TblNode {   /* 散列表结点定义 */    int TableSize; /* 表的最大长度 */    Cell *Cells;   /* 存放散列单元数据的数组 */};HashTable BuildTable(); /* 裁判实现,细节不表 */Position Hash( ElementType Key, int TableSize ){    return (Key % TableSize);}#define ERROR -1Position Find( HashTable H, ElementType Key );int main(){    HashTable H;    ElementType Key;    Position P;    H = BuildTable();     scanf("%d", &Key);    P = Find(H, Key);    if (P==ERROR)        printf("ERROR: %d is not found and the table is full.\n", Key);    else if (H->Cells[P].Info == Legitimate)        printf("%d is at position %d.\n", Key, P);    else        printf("%d is not found.  Position %d is returned.\n", Key, P);    return 0;}/* 你的代码将被嵌在这里 */

输入样例1:(注:-1表示该位置为空。下同。)

11

11 88 21 -1 -1 5 16 7 6 38 10

38

输出样例1:

38 is at position 9.

输入样例2:

11

11 88 21 -1 -1 5 16 7 6 38 10

41

输出样例2:

41 is not found.  Position 3 is returned.

输入样例3:

11

11 88 21 3 14 5 16 7 6 38 10

41

输出样例3:

ERROR: 41 is not found and the table is full.

7-1 整型关键字的散列映射

给定一系列整型关键字和素数P,用除留余数法定义的散列函数H(Key)=Key将关键字映射到长度为P的散列表中。用线性探测法解决冲突。

输入格式:

输入第一行首先给出两个正整数N(≤1000)和P(≥N的最小素数),分别为待插入的关键字总数、以及散列表的长度。第二行给出N个整型关键字。数字间以空格分隔。

输出格式:

在一行内输出每个整型关键字在散列表中的位置。数字间以空格分隔,但行末尾不得有多余空格。

输入样例:

4 5

24 15 61 88

输出样例:

4 0 1 3

三、 设计文档

 

 

四、 源程序

6-2 线性探测法的查找函数

Position Find( HashTable H, ElementType Key ){

    int x=Hash(Key,H->TableSize);

    int a=0;

    while(a<H->TableSize){

        if( H->Cells[x].Info==Empty//题目里还给了个Deleted,没有测试点不鸟它了

           ||H->Cells[x].Data == Key){//可不能等于Legitimate

            return x;

        }

        x=(x+1)%H->TableSize;

        a++;

    }

    return ERROR;

}

 

 

 

 

 

 

7-1 整型关键字的散列映射

//输入第一行首先给出两个正整数N(≤1000)和P(≥N的最小素数)

//分别为待插入的关键字总数、以及散列表的长度。第二行给出N个整型关键字。数字间以空格分隔。

#include <iostream>

using namespace std;

const int MAX = 1000;

 

int main()

{

    int N,P;

    cin >> N >> P; //P为散列表的长度

    int data[MAX];//输入数据

    for(int i = 0;i<N;i++)

    {

        cin >> data[i];

    }

    int HashList[P];

    int index[MAX];

    for(int i = 0;i<P;i++)

        HashList[i] = -1;

    for(int i = 0;i<N;i++)

    {

        int d,j = 0;//j为移动的位置

        d = data[i]%P;

        if(data[i]==HashList[d])

        {

            index[i] = d;

            continue;

        }

        while((HashList[d]!=-1)&&(j<P)&&(data[i]!=HashList[d]))//说明该位置没有元素

        {

            j++;

            d = (d+1)%P;

        }

        HashList[d] = data[i];

        index[i] = d;

    }

    for(int i = 0;i<N-1;i++)

    {

        cout << index[i] << ' ';

    }

    cout << index[N-1];

    return 0;

}

 

posted @ 2022-12-12 15:23  STDU_DREAM  阅读(83)  评论(0编辑  收藏  举报