N—E—E 2021-10-07 15:04 采纳率: 59.5%
浏览 95
已结题

这个再散列函数哪里出问题了?

还有一个问题就是rehash函数中再散列得到的表为什么能直接覆盖到原表上?

#include <stdlib.h>
#include <stdio.h>
struct HashTable;
struct TableEntry;

typedef int ElementType;
typedef int Position;
typedef int Index;
typedef struct HashTable *Tbl;
typedef struct TableEntry Cell;

int Hash(ElementType x,int TblSize);
Tbl Initialize(int tablesize);
Position Find(Tbl T,ElementType x);
void Insert(Tbl T,ElementType x);
void Show(Tbl T);
Tbl Rehash(Tbl T);

enum KindOfEntry{Legitimate,Empty,Delete};

struct HashTable
{
    int size;
    Cell *thecells;
};

struct TableEntry
{
    ElementType value;
    enum KindOfEntry Info;
};

#include "Open adress.h"

int Hash(ElementType x,int TblSize)
{
    return x % TblSize;
}
Tbl Initialize(int tablesize){
    Tbl T = malloc(sizeof(struct HashTable));
    T->size = tablesize;

    T->thecells = malloc(sizeof(struct TableEntry)*T->size);
    for (int i = 0;i < T->size;i++)
    {
        T->thecells[i].Info = Empty;
        T->thecells[i].value = -1;
    }
    return T;
}
Position Find(Tbl T,ElementType x){
    Position CurrentPos;
    int CollisionNum = 0;

    CurrentPos = Hash(x,T->size);
    while (T->thecells[CurrentPos].value != x && T->thecells[CurrentPos].Info != Empty)
    {
        CurrentPos += 2*++CollisionNum - 1;
        if (CurrentPos >= T->size)
            CurrentPos -= T->size;
        
    }
    return CurrentPos;

}
void Insert(Tbl T,ElementType x){
    Position P = Find(T,x);
    if (T->thecells[P].Info = Empty)
    {
        T->thecells[P].Info = Legitimate;
        T->thecells[P].value = x;
    }
}
void Show(Tbl T){
    for (int i = 0;i<T->size;i++)
    {
        if (T->thecells[i].Info != Delete)
            printf("%d\n",T->thecells[i].value);
    }
}
Tbl Rehash(Tbl T){
    int oldsize = T->size;
    Cell *oldcells = T->thecells;

    T = Initialize(2 * oldsize);  //为什么可以覆盖原有的?
    for (int i = 0;i < oldsize;i++)
    {
        if (T->thecells[i].Info = Legitimate)
            Insert(T,T->thecells[i].value);
    }
    free(oldcells);
    return T;
}




int main()
{
    Tbl T = Initialize(10);
    Insert(T,89);
    Insert(T,18);
    Insert(T,49);
    Insert(T,58);
    Insert(T,69);
    Show(T);
    printf("\n");
    Tbl T1 = Rehash(T);
    Show(T1);
    system("pause");
    return 0;

}

img

  • 写回答

1条回答 默认 最新

  • Luuuuk_ 2021-10-08 01:19
    关注
    
    Tbl Rehash(Tbl T) {
        int oldsize = T->size;
        Cell* oldcells = T->thecells;
        T = Initialize(2 * oldsize);  //为什么可以覆盖原有的?
        for (int i = 0; i < oldsize; i++)
        {
            if (oldcells[i].Info == Legitimate)
                Insert(T, oldcells[i].value);
        }
        free(oldcells);
        return T;
    }
    

    img

    是这个结果吗?

    评论

报告相同问题?

问题事件

  • 系统已结题 10月15日
  • 创建了问题 10月7日

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题