| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1309 人关注过本帖
标题:哈希查找的问题
只看楼主 加入收藏
icedream89
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-10-24
收藏
 问题点数:0 回复次数:2 
哈希查找的问题
代码:#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define OK 1
#define ERROR 0
typedef int status;
#define NULLKEY 0
#define N 10
typedef int keytype;
typedef struct
{
    keytype key;
    int ord;

}Elemtype;
#define EQ(a,b) ((a)==(b))
int hashsize[]={11,19,29,37};
int m=0;
typedef struct
{
    Elemtype *elem;
    int count;
    int sizeindex;
}Hashtable;
#define success 1
#define unsuccess 0
#define duplicate -1
status Lnithashtable(Hashtable *h)
{
    int i;
    (*h).count=0
    (*h).sizeindex=0;
    m=hashsize[0];
    (*h).elem=(Elemtype *)malloc(m*sizeof(Elemtype));
    if(!(*h).elem)
        exit(OVERFLOW);
    for(i=0;i<m;i++)
        (*h).elem[i].key=NULLKEY;
    return OK;
}

void Destroyhashtable(Hashtable *h)
{
    free((*h).elem);
    (*h).elem=NULL;
    (*h).count=0;
    (*h).sizeindex=0;
}     

unsigned Hash(keytype k)
{
    return k%m;
}

void collision(int *p,int d)
{
    *p=(*p+d)%m;
}

status Searchhash(Hashtable h,keytype k,int *p,int *c)
{
    *p=Hash(k);
    while(h.elem[*p].key!=NULLKEY&&!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;
}

status Inserthash(Hashtable *h,Elemtype e);
void Recreatehashtable(Hashtable *h)
{
    int i,count=(*h).count;
    Elemtype *p,*elem=(Elemtype *)malloc(count *sizeof(Elemtype));
    p=elem;
    printf("重建哈希表\n");
    for(i=0;i<m;i++)
        if(((*h).elem+i)->key!=NULLKEY);
            *p++=((*h).elem+i);
        (*h).count=0;
        (*h).sizeindex++;
        m=hashsize[(*h).sizeindex];
        p=(Elemtype *)realloc((*h).elem,m*sizeof(Elemtype));
        if(!p)
            exit(OVERFLOW);
        (*h).elem=p;
        for(i=0;i<m;i++)
            (*h).elem[i].key=NULLKEY;
        for(p=elem;p<elem+count;p++)
            Inserthash(h,*p);
}

status Inserthash(Hashtable *h,Elemtype e)
{
    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);
    return ERROR;

}

void Traversehash(Hashtable h,void(*Vi)(int,Elemtype))
{
    int i;
    printf("哈希地址0~%d\n",m-1);
    for(i=0;i<m;i++)
        if(h.elem[i].key!=NULLKEY)
            Vi(i,h.elem[i]);
}

status Find(Hashtable h,keytype k,int *p)
{
    int c=0;
    *p=Hash(k);
    while(h.elem[*p].key!=NULLKEY&&!EQ(k,h.elem[*p].key))
    {
        c++;
        if(c<m)
            collision(p,c);
        else
            return success;
    }
    if(EQ(k,h.elem[*p].key))
        return success;
    else
        return unsuccess;
}

void print(int p,Elemtype r)
{
    printf("address=%d(%d,%d)\n",p,r.key,r.ord);

}

void main()
{
    Elemtype r[N]={{17,1},{60,2},{29,3},{38,4},{1,5},{2,6},{3,7},{4,8},{60,9},{13,10}};
    Hashtable h;
    int i,p;
    status j;
    keytype k;
    Lnithashtable(&h);
    for(i=0;i<N-1;i++)
    {
        j=Inserthash(&h,r[i]);
        if(j==duplicate)
            printf("表中已有关键字为%d录,无法再插入记录(%d,%d)\n",r[i].key,r[i].key,r[i].ord);
    }
    printf("按哈希地址的顺序遍历哈希表:\n");
    Traversehash(h,print);
    printf("请输入待查找记录的关键字: ");
    scanf("%d",&k);
    j=Find(h,k,&p);
    if(j=success)
        print(p,h.elem[p]);
    else
        printf("没找到\n");
    j=Inserthash(&h,r[i]);
    if(j==ERROR)
        j=Inserthash(&h,r[i]);
    printf("按哈希地址的顺序遍历重建后的哈希表: \n");
    Traversehash(h,print);
    printf("请输入待查找的关键字: ");
    scanf("%d",&k);
    j=Find(h,k,&p);
    if(j==success)
        print(p,h.elem[p]);
    else
        printf("没找到\n");
    Destroyhashtable(&h);
}

问题:--------------------Configuration: pl - Win32 Debug--------------------
Compiling...
pl.cpp
H:\C语言编程实践文件夹\pl\pl.cpp(32) : error C2228: left of '.sizeindex' must have class/struct/union type
H:\C语言编程实践文件夹\pl\pl.cpp(86) : warning C4390: ';' : empty controlled statement found; is this the intent?
H:\C语言编程实践文件夹\pl\pl.cpp(86) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'Elemtype *' (or there is no acceptable conversion)
执行 cl.exe 时出错.

pl.exe - 1 error(s), 0 warning(s)
搜索更多相关主题的帖子: include success status count 
2011-08-09 01:06
laznrbfe
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:482
专家分:1599
注 册:2011-5-22
收藏
得分:0 
我只能改到没有编译错误,但不能保证运行没有错误
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define OK 1
#define ERROR 0
typedef int status;
#define NULLKEY 0
#define N 10
typedef int keytype;
typedef struct
{
    keytype key;
    int ord;

}Elemtype;
#define EQ(a,b) ((a)==(b))
int hashsize[]={11,19,29,37};
int m=0;
typedef struct
{
    Elemtype *elem;
    int count;
    int sizeindex;
}Hashtable;
#define success 1
#define unsuccess 0
#define duplicate -1
status Lnithashtable(Hashtable *h)
{
    int i;
    (*h).count=0;
    (*h).sizeindex=0;
    m=hashsize[0];
    (*h).elem=(Elemtype *)malloc(m*sizeof(Elemtype));
    if(!(*h).elem)
        exit(OVERFLOW);
    for(i=0;i<m;i++)
        (*h).elem[i].key=NULLKEY;
    return OK;
}

void Destroyhashtable(Hashtable *h)
{
    free((*h).elem);
    (*h).elem=NULL;
    (*h).count=0;
    (*h).sizeindex=0;
}     

unsigned Hash(keytype k)
{
    return k%m;
}

void collision(int *p,int d)
{
    *p=(*p+d)%m;
}

status Searchhash(Hashtable h,keytype k,int *p,int *c)
{
    *p=Hash(k);
    while(h.elem[*p].key!=NULLKEY&&!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;
}

status Inserthash(Hashtable *h,Elemtype e);
void Recreatehashtable(Hashtable *h)
{
    int i,count=(*h).count;
    Elemtype *p,*elem=(Elemtype *)malloc(count *sizeof(Elemtype));
    p=elem;
    printf("重建哈希表\n");
    for(i=0;i<m;i++)
        if(((*h).elem+i)->key!=NULLKEY)/////////////////////
            *p++=*(elem+i);///////////////////////////////////
        (*h).count=0;
        (*h).sizeindex++;
        m=hashsize[(*h).sizeindex];
        p=(Elemtype *)realloc((*h).elem,m*sizeof(Elemtype));
        if(!p)
            exit(OVERFLOW);
        (*h).elem=p;
        for(i=0;i<m;i++)
            (*h).elem[i].key=NULLKEY;
        for(p=elem;p<elem+count;p++)
            Inserthash(h,*p);
}

status Inserthash(Hashtable *h,Elemtype e)
{
    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);
    return ERROR;

}

void Traversehash(Hashtable h,void(*Vi)(int,Elemtype))
{
    int i;
    printf("哈希地址0~%d\n",m-1);
    for(i=0;i<m;i++)
        if(h.elem[i].key!=NULLKEY)
            Vi(i,h.elem[i]);
}

status Find(Hashtable h,keytype k,int *p)
{
    int c=0;
    *p=Hash(k);
    while(h.elem[*p].key!=NULLKEY&&!EQ(k,h.elem[*p].key))
    {
        c++;
        if(c<m)
            collision(p,c);
        else
            return success;
    }
    if(EQ(k,h.elem[*p].key))
        return success;
    else
        return unsuccess;
}

void print(int p,Elemtype r)
{
    printf("address=%d(%d,%d)\n",p,r.key,r.ord);

}

void main()
{
    Elemtype r[N]={{17,1},{60,2},{29,3},{38,4},{1,5},{2,6},{3,7},{4,8},{60,9},{13,10}};
    Hashtable h;
    int i,p;
    status j;
    keytype k;
    Lnithashtable(&h);
    for(i=0;i<N-1;i++)
    {
        j=Inserthash(&h,r[i]);
        if(j==duplicate)
            printf("表中已有关键字为%d录,无法再插入记录(%d,%d)\n",r[i].key,r[i].key,r[i].ord);
    }
    printf("按哈希地址的顺序遍历哈希表:\n");
    Traversehash(h,print);
    printf("请输入待查找记录的关键字: ");
    scanf("%d",&k);
    j=Find(h,k,&p);
    if(j=success)
        print(p,h.elem[p]);
    else
        printf("没找到\n");
    j=Inserthash(&h,r[i]);
    if(j==ERROR)
        j=Inserthash(&h,r[i]);
    printf("按哈希地址的顺序遍历重建后的哈希表: \n");
    Traversehash(h,print);
    printf("请输入待查找的关键字: ");
    scanf("%d",&k);
    j=Find(h,k,&p);
    if(j==success)
        print(p,h.elem[p]);
    else
        printf("没找到\n");
    Destroyhashtable(&h);
}
2011-08-09 09:08
icedream89
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-10-24
收藏
得分:0 
回复 2楼 laznrbfe
基本发现错误了,多谢了

[ 本帖最后由 icedream89 于 2011-8-9 14:36 编辑 ]
2011-08-09 14:33
快速回复:哈希查找的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019248 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved