| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 600 人关注过本帖
标题:完了 写了主函数 又执行不了help
取消只看楼主 加入收藏
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
结帖率:97.44%
收藏
已结贴  问题点数:20 回复次数:4 
完了 写了主函数 又执行不了help
程序代码:
/*
*哈希表 开放定址法
*/
#include<stdio.h>
#include<stdlib.h>

#define MinTableSize 10

typedef int ElemType;

enum KindOfEntry{Legitimate,Empty,Deleted};

typedef struct HashEntry
{
    ElemType element;
    enum KindOfEntry info;
}Cell;

/* Cell *TheCells will be an array of */
/* HashEntry cells, allocated later */
typedef struct HashTbl
{
    int TableSize;
    Cell *TheCells;
}*HashTable;

typedef  int Index;
typedef Index Position;

/* Return next prime; assume N >= 10 */
int NextPrime(int N)
{
    int i;

    if(N%2==0)
        N++;
    for(;;N+=2)
    {
        for(i=3;i*i<=N;i+=2)
            if(N%i==0)
                return 0;
        return N;
    }
}

/*Hash function for ints*/
Index Hash(ElemType Key,int TableSize)
{
    return Key%TableSize;
}

HashTable InitializeTable(int TableSize)
{
    HashTable H;
    int i;

    if(TableSize<MinTableSize)
    {
        printf("Table size too small");
        return NULL;
    }

    /*Allocate table*/
    H=(HashTable)malloc(sizeof(struct HashTbl));
    if(NULL==H)
        printf("Out of space!!!\n");

    H->TableSize=NextPrime(TableSize);

    /*Allocate array of cells*/
    H->TheCells=(Cell *)malloc(sizeof(Cell)*H->TableSize);
    if(NULL==H->TheCells)
    {
        printf("Out of space!!!\n");
        free(H);
    }

    for(i=0;i<H->TableSize;i++)
    {
        H->TheCells[i].info=Empty;
        H->TheCells[i].element=0;
    }
   
    return H;
}
Position Find(ElemType Key,HashTable H)
{
    Position CurrentPos;
    int CollisionNum;

    CollisionNum=0;
    CurrentPos=Hash(Key,H->TableSize);
    while( H->TheCells[ CurrentPos ].info != Empty &&
           H->TheCells[ CurrentPos ].element != Key )/* Probably need strcmp!! */
    {
        CurrentPos+=2*++CollisionNum-1;
        if(CurrentPos>=H->TableSize)
            CurrentPos-=H->TableSize;
    }
    return CurrentPos;
}
void Insert(ElemType Key,HashTable H)
{
    Position Pos;

    Pos=Find(Key,H);
    if(H->TheCells[Pos].info!=Legitimate)
    {
        H->TheCells[Pos].info=Legitimate;/*Ok to insert here*/
        H->TheCells[Pos].element=Key;/*Probably need strcpy*/
    }
}
HashTable ReHash(HashTable H)
{
    int i,OldSize;
    Cell *OldCells;

    OldCells=H->TheCells;
    OldSize=H->TableSize;

    /*Get a new empty table*/
    H=InitializeTable(2*OldSize);

    /*Scan through old table reinserting into new*/
    for(i=0;i<OldSize;i++)
        if(OldCells[i].info==Legitimate)
            Insert(OldCells[i].element,H);

    free(OldCells);

    return H;
}
void traverseHash(HashTable H,int len)
{
    int i;

    printf("哈希地址0~%d\n",len-1);
    for(i=0;i<len;i++)
    {
        if(H->TheCells[i].info=Legitimate)//有数据
            printf("address=%d value=%d\n",i,H->TheCells[i].element);
           
    }
}
void DestroyTable(HashTable H)
{
    free(H->TheCells);
    free(H);
}
int main()
{
    HashTable H;
   
    int array[]={19,14,23,01,68,20,84,27,55,11,10,79};
    int len=sizeof(array)/sizeof(array[0]);
    int i;
//    ElemType k;
            
    H=InitializeTable(len);

    for(i=0;i<len;i++)
    {
        Insert(array[i],H);   
    }
    traverseHash(H,len);
    printf("\n\n");

    return 0;
}
2011-08-17 22:13
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
分析了下  问题在插入的部分
可是 调了好久 应该没错 大家帮忙看下啊
2011-08-17 22:33
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
谢谢啦
没有哦  哈希表 部分学的不好
2011-08-18 10:23
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
程序代码:
/*
*哈希表 开放定址法
*/
#include<stdio.h>
#include<stdlib.h>

#define MinTableSize 10

typedef int ElemType;

enum KindOfEntry{Legitimate,Empty,Deleted};

typedef struct HashEntry
{
    ElemType element;
    enum KindOfEntry info;
}Cell;

/* Cell *TheCells will be an array of */
/* HashEntry cells, allocated later */
typedef struct HashTbl
{
    int TableSize;
    Cell *TheCells;
}*HashTable;

typedef  int Index;
typedef Index Position;

/* Return next prime; assume N >= 10 */
int NextPrime(int N)
{
    int i;

    if(N%2==0)
        N++;
    for(;;N+=2)
    {
        for(i=3;i*i<=N;i+=2)
            if(N%i==0)
                return 0;
        return N;
    }
}

/*Hash function for ints*/
Index Hash(ElemType Key,int TableSize)
{
    return Key%TableSize;
}

HashTable InitializeTable(int TableSize)
{
    HashTable H;
    int i;

    if(TableSize<MinTableSize)
    {
        printf("Table size too small");
        exit(-1);
    }

    /*Allocate table*/
    H=(HashTable)malloc(sizeof(struct HashTbl));
    if(NULL==H)
    {
        printf("Out of space!!!\n");
        exit(-1);
    }
        

    H->TableSize=NextPrime(TableSize);

    /*Allocate array of cells*/
    H->TheCells=(Cell *)malloc(sizeof(Cell)*H->TableSize);
    if(NULL==H->TheCells)
    {
        printf("Out of space!!!\n");
        free(H);
    }

    for(i=0;i<H->TableSize;i++)
    {
        H->TheCells[i].info=Empty;
        //H->TheCells[i].element=0;
    }
   
    return H;
}

Position Find(ElemType Key,HashTable H)
{
    Position CurrentPos;
    int CollisionNum;

    CollisionNum=0;
    CurrentPos=Hash(Key,H->TableSize);
    while( H->TheCells[ CurrentPos ].info != Empty &&
           H->TheCells[ CurrentPos ].element != Key )/* Probably need strcmp!! */
    {
        CurrentPos+=2*++CollisionNum-1;
        if(CurrentPos>=H->TableSize)
            CurrentPos-=H->TableSize;
    }
    return CurrentPos;
}

void Insert(ElemType Key,HashTable H)
{
    Position Pos;

    Pos=Find(Key,H);
    if(H->TheCells[Pos].info!=Legitimate)
    {
        H->TheCells[Pos].info=Legitimate;/*Ok to insert here*/
        H->TheCells[Pos].element=Key;/*Probably need strcpy*/
    }
}

HashTable ReHash(HashTable H)
{
    int i,OldSize;
    Cell *OldCells;

    OldCells=H->TheCells;
    OldSize=H->TableSize;

    /*Get a new empty table*/
    H=InitializeTable(2*OldSize);

    /*Scan through old table reinserting into new*/
    for(i=0;i<OldSize;i++)
        if(OldCells[i].info==Legitimate)
            Insert(OldCells[i].element,H);

    free(OldCells);

    return H;
}

void traverseHash(HashTable H,int len)
{
    int i;

    printf("哈希地址0~%d\n",len);
    for(i=0;i<len;i++)
    {
        if(H->TheCells[i].info!=Empty)//有数据
            printf("address=%d value=%d\n",i,H->TheCells[i].element);
           
    }
}

void DestroyTable(HashTable H)
{
    free(H->TheCells);
    free(H);
}

int main()
{
    HashTable H;
   
    int array[]={19,14,23,01,68,20,84,27,55,11,10,79};
    int len=sizeof(array)/sizeof(array[0]);
    int i;
    ElemType k;
    Position p;
            
    H=InitializeTable(len-1);//插入前len-1个记录
    for(i=0;i<len-1;i++)
    {
        Insert(array[i],H);   
    }
    traverseHash(H,len-1);
    printf("\n\n");

    printf("please input the value which need find:");
    scanf("%d",&k);
    p=Find(k,H);
    if(p>=0)
        printf("address=%d  value=%d\n",p,H->TheCells[p].element);
    else
        printf("cannot find the value!");
    printf("\n\n");

    H=ReHash(H);
    traverseHash(H,2*len);
    printf("\n\n");

    printf("please input the value which need find:");
    scanf("%d",&k);
    p=Find(k,H);
    if(p>=0)
        printf("address=%d  value=%d\n",p,H->TheCells[p].element);
    else
        printf("cannot find the value!");
    printf("\n\n");

    printf("free the table\n");
    DestroyTable(H);
    printf("it's done!!!");
    printf("\n\n");

    return 0;
}
重建之后 元素79 依然插不进去
2011-08-18 13:08
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
严版的我写过  
上面的我是在数据结构算法与分析里看到的 感觉那个更简练写 所以还是想 把那个搞懂
2011-08-18 21:10
快速回复:完了 写了主函数 又执行不了help
数据加载中...
 
   



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

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