所以这两种写法有什么不一样???

今天做线性探索法的题目(题目在下面),又遇到令我很费解的事情。下面这两种写法究竟有什么不一样???

Position Find(HashTable H, ElementType Key)
{    
   Position NewPos;

    int CNum;

    for(CNum = 0, NewPos = Hash(Key, H->TableSize); CNum<H->TableSize; CNum++)
    {
        if((H->Cells[NewPos].Data == Key) || (H->Cells[NewPos].Info == Empty))
             return NewPos;
        else
        {
            NewPos = (NewPos+CNum) % H->TableSize;
        }
    }
    return ERROR;

}       

这个变成这样

测试点提示结果耗时内存
0同sample1,找到答案错误1 ms128KB
1同sample2,找不到答案正确1 ms128KB
2同sample3,找不到并且表满答案正确1 ms256KB
3大规模数据,找到答案错误22 ms988KB
4大规模数据,找不到答案正确21 ms968KB

但改写成这样后:

Position Find(HashTable H, ElementType Key)
{    
   Position NewPos;

    int CNum;

    for(CNum = 0, NewPos = Hash(Key, H->TableSize); CNum<H->TableSize; CNum++)
    {
        if(H->Cells[NewPos].Data == Key)
             return NewPos;
        else
        {
            NewPos = (NewPos+CNum) % H->TableSize;
        }
    }
    for(CNum = 0, NewPos = Hash(Key, H->TableSize); CNum<H->TableSize; CNum++)
    {
        if(H->Cells[NewPos].Info == Empty)
             return NewPos;
        else
        {
            NewPos = (NewPos+CNum) % H->TableSize;
        }
    }
    return ERROR;

}       
就全AC了。为什么这两个判断条件写在一起就不行呀??



习题5.10 线性探测法的查找函数(20 分)

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

函数接口定义:

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 -1
Position 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值