实验7:查找

第1关:实现折半查找

任务描述

本关要求通过补全函数BSL_FindKey来实现在已排序的顺序表中查找关键码值为key的结点并返回该结点的编号。

相关知识

折半查找通常是针对顺序存储的线性表,线性表的结点按关键码从小到大排序,后面称之为折半查找的顺序表。为了简化讨论,假设折半查找的顺序表中每个结点只含一个关键码,关键码为整数。图 1 给出了一个存储了 4 个关键码的折半查找的顺序表的存储结构图。

下面描述了线性表顺序存储的一种实现方案。该实现方案的示意图为:

指针pkey是存储关键码的连续空间的起始地址,顺序表中当前的关键码的个数由len给出,该顺序表中最多可存储max个关键码。

pkeylenmax组织成一个结构,该结构定义为:

 
  1. struct BSeqList{
  2. int* pkey; // 指向关键码数组的指针
  3. int len; // 当前元素个数
  4. int max; // 线性表的最大元素数
  5. };

只要给定指向该结构的一指针blist,就可对线性表进行操作。

对折半查找的顺序表定义如下操作,各个操作函数的功能详见下面给出的代码文件 BSlist.cpp 的代码框架:

 
  1. BSeqList* BSL_Create(int size);
  2. void BSL_Free(BSeqList* blist);
  3. int BSL_InsKey(BSeqList* blist, int key);
  4. int BSL_FindKey(BSeqList* blist, int key);
  5. int BSL_DelKey(BSeqList* blist, int key);
  6. void BSL_Print(BSeqList* blist);
编程要求

本关的编程任务是补全 step1/BSlist.cpp 文件中的BSL_FindKey函数,以实现在已排序的顺序表中查找关键码值为key的结点并返回该结点的编号。当返回值大于等于 0 时则表示找到值为key的结点的编号,若为 -1 则表示没有找到。

  • 具体请参见后续测试样例。

本关涉及的代码文件 BSlist.cpp 的代码框架如下:

 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "bsList.h"
  4. BSeqList* BSL_Create(int size)
  5. //创建一个顺序表
  6. //与BSL_Free()配对
  7. {
  8. BSeqList* blist=(BSeqList*)malloc(sizeof(BSeqList));
  9. blist->pkey = (int*)malloc(sizeof(int)*size);
  10. blist->max=size;
  11. blist->len=0;
  12. return blist;
  13. }
  14. void BSL_Free(BSeqList* blist)
  15. //释放/删除顺序表
  16. //与BSL_Create()配对
  17. {
  18. free(blist->pkey);
  19. free(blist);
  20. }
  21. int BSL_FindKey(BSeqList* blist, int key)
  22. //在排序的顺序表中查找关键码值为key的结点,返回结点的编号
  23. //返回值大于等于0时表示找到值为key的结点的编号,-1表示没有找到
  24. {
  25. // 请在此添加代码,补全函数BSL_FindKey
  26. /********** Begin *********/
  27. /********** End **********/
  28. }
  29. int BSL_InsKey(BSeqList* blist, int key)
  30. //在排序的顺序表中插入一个值为key的结点
  31. //返回值大于等于0表示插入的位置, -1表示表满(无法插入)
  32. {
  33. /********** Begin *********/
  34. /********** End **********/
  35. }
  36. int BSL_DelKey(BSeqList* blist, int key)
  37. //在排序的顺序表中删除值为key的结点,
  38. //存在值为x的结点则返回结点编号, 未找到返回-1
  39. {
  40. /********** Begin *********/
  41. /********** End **********/
  42. }
  43. void BSL_Print(BSeqList* blist)
  44. //打印整个顺序表
  45. {
  46. if (blist->len==0) {
  47. printf("The list is empty.\n");
  48. return;
  49. }
  50. printf("The list contains: ");
  51. for (int i=0; i<blist->len; i++) {
  52. printf("%d ", blist->pkey[i]);
  53. }
  54. printf("\n");
  55. }
测试说明

本关的测试文件是 step1/Main.cpp ,测试过程如下:

  1. 平台编译 step1/Main.cpp ,然后链接相关程序库并生成 exe 可执行文件;

  2. 平台运行该 exe 可执行文件,并以标准输入方式提供测试输入;

  3. 平台获取该 exe 可执行文件的输出,然后将其与预期输出对比,如果一致则测试通过;否则测试失败。

输入输出格式说明

输入格式: 首先输入一个正整数max,创建一个最多可存储max个元素的表。 然后输入多个操作:如果输入 “insert” ,则后面跟一个数x,表示将x插入;如果输入 “delete” ,则后面跟一个数x,表示将x删除;如果输入 “end” ,表示输入结束。

输出格式: 输出表的长度,然后从表头到表尾依次输出各元素。

以下是平台对 step1/Main.cpp 的样例测试集:

样例输入:

9

insert 9

insert 8

insert 89

insert 11

insert 22

insert 13

delete 11

delete 5

end

样例输出:

list length: 5

The list contains: 8 9 13 22 89

上答案:

/*************************************************************
    date: April 2009
    copyright: Zhu En
    DO NOT distribute this code.
**************************************************************/
//折半查找的顺序表 实现文件
//每个结点的数据是关键码
//
#include <stdio.h>
#include <stdlib.h>
#include "BSlist.h"

BSeqList* BSL_Create(int size)
//创建一个顺序表
//与BSL_Free()配对
{
    BSeqList* blist=(BSeqList*)malloc(sizeof(BSeqList));
    blist->pkey = (int*)malloc(sizeof(int)*size);
    blist->max=size;
    blist->len=0;
    return blist;
}

void BSL_Free(BSeqList* blist)
//释放/删除顺序表
//与BSL_Create()配对
{
    free(blist->pkey);
    free(blist);
}

int BSL_FindKey(BSeqList* blist, int key)
//在排序的顺序表中查找关键码值为key的结点,返回结点的编号
//返回值大于等于0时表示找到值为key的结点的编号,-1表示没有找到
{
    /*请在BEGIN和END之间实现你的代码*/
    /*****BEGIN*****/
       int l=0,h=blist->len,mid;
    while(l<=h)
    {
        mid=(l+h)/2;
        if(key==blist->pkey[mid])return mid;
        else 
        {
            if(key<blist->pkey[mid]) h=mid-1;
        else 
        {
            l=mid+1;
        }
        }
    }
    return -1;
    /******END******/
    /*请不要修改[BEGIN,END]区域外的代码*/
}

int BSL_InsKey(BSeqList* blist, int key)
//在排序的顺序表中插入一个值为key的结点
//返回值大于等于0时表示插入的位置, -1表示表满(无法插入)
{

    /*请在BEGIN和END之间实现你的代码*/
    /*****BEGIN*****/
     if(blist->len==blist->max)return -1;
    int l=0,r=blist->len-1,m;
    while(l<=r)
    {
        m=(l+r)>>1;
        if(key==blist->pkey[m])return -2;
        if(key<blist->pkey[m])r=m-1;
        else l=m+1;
    }
    for(int i=blist->len;i>l;i--)
    {
        blist->pkey[i]=blist->pkey[i-1];
    }
    blist->pkey[l]=key;
    blist->len++;
    return l;
    /******END******/
    /*请不要修改[BEGIN,END]区域外的代码*/
}

int BSL_DelKey(BSeqList* blist, int key)
//在排序的顺序表中删除值为key的结点, 
//存在值为x的结点则返回结点编号, 未找到返回-1
{
    /*请在BEGIN和END之间实现你的代码*/
    /*****BEGIN*****/
   int k=BSL_FindKey(blist, key);
   if(k<0)return -1;
   int i=k;
    while(i<blist->len-1)
    {
        blist->pkey[i]=blist->pkey[i+1];
        i++;
    }
    blist->len--;
    return -1;
    /******END******/
    /*请不要修改[BEGIN,END]区域外的代码*/
}

void BSL_Print(BSeqList* blist)
//打印整个顺序表
{
    if (blist->len==0) {
        printf("The list is empty.\n");
        return;
    }
    printf("The list contains: ");
    for (int i=0; i<blist->len; i++) {
        printf("%d  ", blist->pkey[i]);
    }
    printf("\n");
}

第2关:实现散列查找

任务描述

本关要求通过补全函数ILH_InsKeyILH_DelKey来分别实现插入和删除操作。

相关知识

本关讨论散列存储,散列函数使用除留余数法,冲突解决方法采用独立链表地址法。假设有 8 个关键码: 7 , 15 , 23 , 31 , 12 , 14 , 10 , 17 ,采用散列函数hash(key)=key%7,其存储结构图如图 1 所示,它由 7 个独立链表组成,散列值相同的关键码在同一个链表里,独立链表的头结点组成散列表,一共 7 行,编号 0 , 1 , … , 6 。独立链表的每个结点是一个 struct HNode 结构,其定义如下:

 
  1. struct HNode {
  2. int key; //假设关键码为整数
  3. HNode* next;
  4. };

在散列表中,如果表项的key字段等于 0 (假设有效的关键码值不等于 0 ),则表示该行是一条空链表,例如图 1 中编号为 4 和编号为 6 的行。

散列表的开始地址保存在pn中,散列表的行数为n(图 1 中,n=7),将pnn组织成结构:

 
  1. struct LHTable {
  2. HNode* pn; //指向散列表,每个表结点是独立链表的表头结点
  3. int n; //散列表的长度,一般取(小于等于数据个数的最大)质数
  4. };

定义如下操作,各操作函数的功能详见下面给出的代码文件 indLnkHash.cpp 的代码框架:

 
  1. LHTable* ILH_Create(int n);
  2. void ILH_Free(LHTable* pt);
  3. bool ILH_InsKey(LHTable* pt, int x);
  4. bool ILH_FindKey(LHTable* pt, int x);
  5. bool ILH_DelKey(LHTable* pt, int x);
  6. void ILH_Print(LHTable *pt);
编程要求

本关的编程任务是补全 step2/indLnkHash.cpp 文件中的ILH_InsKeyILH_DelKey函数来分别实现插入和删除操作。

  • 具体请参见后续测试样例。

本关涉及的代码文件 indLnkHash.cpp 的代码框架如下:

 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "indLnkHash.h"
  5. LHTable* ILH_Create(int n)
  6. //创建散列表, n为表长度,最佳取值:n取小于等于数据个数的最大质数
  7. {
  8. HNode* pn=(HNode*)malloc(sizeof(HNode)*n);
  9. for (int i=0; i<n; i++) {
  10. pn[i].key=0;
  11. pn[i].next=NULL;
  12. }
  13. LHTable* pt=(LHTable*)malloc(sizeof(LHTable));
  14. pt-> pn=pn;
  15. pt->n=n;
  16. return pt;
  17. }
  18. void ILH_Free(LHTable* pt)
  19. //释放散列表
  20. {
  21. if (pt==NULL) return;
  22. for (int i=0; i<pt->n; i++) {
  23. HNode* curr=pt->pn[i].next;
  24. while (curr) {
  25. HNode* next=curr->next;
  26. free(curr);
  27. curr=next;
  28. }
  29. }
  30. free(pt->pn);
  31. free(pt);
  32. }
  33. bool ILH_InsKey(LHTable* pt, int x)
  34. //插入关键码x
  35. //返回true,表示插入成功
  36. //返回false,表示插入失败(关键码已经存在)
  37. {
  38. // 请在此添加代码,补全函数ILH_InsKey
  39. /********** Begin *********/
  40. /********** End **********/
  41. }
  42. bool ILH_FindKey(LHTable* pt, int x)
  43. //查找关键码x
  44. //返回true表示找到
  45. //返回false表示没找到
  46. {
  47. /*请在BEGIN和END之间实现你的代码*/
  48. /*****BEGIN*****/
  49. /******END******/
  50. /*请不要修改[BEGIN,END]区域外的代码*/
  51. }
  52. bool ILH_DelKey(LHTable* pt, int x)
  53. //删除关键码
  54. //返回true表示该关键码存在,且成功删除
  55. //返回false表示该关键码不存在
  56. {
  57. // 请在此添加代码,补全函数ILH_DelKey
  58. /********** Begin *********/
  59. /********** End **********/
  60. }
  61. void ILH_Print(LHTable *pt)
  62. {
  63. for (int i=0; i<pt->n; i++) {
  64. printf("%5d: ", i);
  65. if (pt->pn[i].key) {
  66. printf("%d ", pt->pn[i].key);
  67. HNode* curr=pt->pn[i].next;
  68. while (curr) {
  69. printf("-> %d ", curr->key);
  70. curr=curr->next;
  71. }
  72. printf("\n");
  73. }
  74. else
  75. printf("-\n");
  76. }
  77. }
测试说明

本关的测试文件是 step2/Main.cpp ,测试过程如下:

  1. 平台编译 step2/Main.cpp ,然后链接相关程序库并生成 exe 可执行文件;

  2. 平台运行该 exe 可执行文件,并以标准输入方式提供测试输入;

  3. 平台获取该 exe 可执行文件的输出,然后将其与预期输出对比,如果一致则测试通过;否则测试失败。

输入输出格式说明

输入格式: 首先输入一个正整数n,创建一个长n的散列表。 然后输入多个操作:如果输入 “insert” ,则后面跟一个数x,表示将x插入;如果输入 “delete” ,则后面跟一个数x,表示将x删除;如果输入 “end” ,表示输入结束。

输出格式: 输出n个独立链表。

以下是平台对 step2/Main.cpp 的样例测试集:

样例输入:

11

insert 54

insert 77

insert 94

insert 89

insert 14

insert 45

insert 76

insert 23

insert 43

insert 47

end

样例输出:

0: 77

1: 89 -> 45 -> 23

2: -

3: 14 -> 47

4: -

5: -

6: 94

7: -

8: -

9: -

10: 54 -> 76 -> 43

上答案:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "indLnkHash.h"

LHTable* ILH_Create(int n)
//创建散列表, n为表长度,最佳取值:n取小于等于数据个数的最大质数
{
    HNode* pn=(HNode*)malloc(sizeof(HNode)*n);
    for (int i=0; i<n; i++) {
        pn[i].key=0;
        pn[i].next=NULL;
    }
    LHTable* pt=(LHTable*)malloc(sizeof(LHTable));
    pt-> pn=pn;
    pt->n=n;
    return pt;
}

void ILH_Free(LHTable* pt)
//释放散列表
{
    if (pt==NULL) return;
    for (int i=0; i<pt->n; i++) {
        HNode* curr=pt->pn[i].next;
        while (curr) {
            HNode* next=curr->next;
            free(curr);
            curr=next;
        }
    }
    free(pt->pn);
    free(pt);
}

bool ILH_InsKey(LHTable* pt, int x)
//插入关键码x
//返回true,表示插入成功
//返回false,表示插入失败(关键码已经存在)
{
    /*请在BEGIN和END之间实现你的代码*/
    /*****BEGIN*****/
      int m=x%pt->n;  
    if (pt->pn[m].key==0) 
    {  
        pt->pn[m].key=x;  
        return true;  
    }  
    else if (pt->pn[m].key==x)   
        return false;  
    HNode* prev=&(pt->pn[m]);  
    HNode* c=pt->pn[m].next;  
    while (c && c->key!=x) {prev=c; c=c->next;}  
    if (c) return  false;  
    HNode* pnode=(HNode*)malloc(sizeof(HNode));  
    pnode->key=x;  
    pnode->next=NULL;//pt->pn[d].next;  
    prev->next=pnode;  
    return true;  
    /******END******/
    /*请不要修改[BEGIN,END]区域外的代码*/
}

bool ILH_FindKey(LHTable* pt, int x)
//查找关键码x
//返回true表示找到
//返回false表示没找到
{
    /*请在BEGIN和END之间实现你的代码*/
    /*****BEGIN*****/
     int d=x%pt->n;  
    if (pt->pn[d].key==0) {  
        return false;  
    }  
    else if (pt->pn[d].key==x)   
        return true;
    HNode* curr=pt->pn[d].next;  
    while (curr && curr->key!=x) curr=curr->next;
    if (curr) return  true;  
    else return false;  
    /******END******/
    /*请不要修改[BEGIN,END]区域外的代码*/
}

bool ILH_DelKey(LHTable* pt, int x)
//删除关键码
//返回true表示该关键码存在,且成功删除
//返回false表示该关键码不存在
{
    /*请在BEGIN和END之间实现你的代码*/
    /*****BEGIN*****/
      int m=x%pt->n;//关键码x的散列值d  
    if (pt->pn[m].key==0) {  
        return false;  
    }  
    else if (pt->pn[m].key==x)  {  
        if (pt->pn[m].next ==NULL)   
            pt->pn[m].key=0;  
        else {  
            HNode* first=pt->pn[m].next;  
            pt->pn[m].key=first->key;  
            pt->pn[m].next=first->next;  
            free(first);  
        }  
        return true;  
    }  
    HNode* prev=&(pt->pn[m]);  
    HNode* c=pt->pn[m].next;  
    while (c && c->key!=x) {prev=c; c=c->next;}  
    if (c==NULL) return false;  
    prev->next=c->next;  
    free(c);  
    return true;  
    /******END******/
    /*请不要修改[BEGIN,END]区域外的代码*/
}

void ILH_Print(LHTable *pt)
{
    for (int i=0; i<pt->n; i++) {
        printf("%5d:", i);
        if (pt->pn[i].key) {
            printf("%d", pt->pn[i].key);
            HNode* curr=pt->pn[i].next;
            while (curr) {
                printf("->%d", curr->key);
                curr=curr->next;
            }
            printf("\n");
        }
        else 
            printf("-\n");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Galaxy*★

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值