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

#define MinTableSize 10

typedef int ElemType;
typedef unsigned int Index;

typedef struct ListNode
{
    ElemType element;
    struct    ListNode *next;
}*Position;

typedef Position List;

typedef struct HashTbl
{
    int TableSize;
     List *TheLists;
}*HashTable;

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!\n");
        return NULL;
    }
   
    /*Allocate table*/
    H=(HashTable)malloc(sizeof(struct HashTbl));
    if(NULL==H)
        printf("Out of space!!!\n");

    H->TableSize=NextPrime(TableSize);

    /*Allocate list  headers*/
    for(i=0;i<H->TableSize;i++)
    {
        H->TheLists[i]=(Position)malloc(sizeof(struct ListNode));
        if(H->TheLists[i]==NULL)
            printf("Out of space!!!\n");
        else
            H->TheLists[i]->next=NULL;
    }
    return H;
}

Position Find(ElemType Key,HashTable H)
{
    Position p;
    List L;

    L=H->TheLists[Hash(Key,H->TableSize)];
    p=L->next;
    while(p!=NULL&&p->element!=Key)/*Probably need strcmp!!*/
        p=p->next;

    return p;
}

void Insert(ElemType Key,HashTable H)
{
    Position pos,newCell;
    List L;

    pos=Find(Key,H);
    if(NULL==pos)/*Key is not found*/
    {
        newCell=(Position)malloc(sizeof(struct ListNode));
        if(NULL==newCell)
            printf("Out of space!!!");
        else
        {
            L=H->TheLists[Hash(Key,H->TableSize)];
            newCell->next=L->next;
            newCell->element=Key;/*Probably need strcpy*/
            L->next=newCell;
        }
    }
}

ElemType Retrieve(Position p)
{
    return p->element;
}


void DestroyTable(HashTable H)
{
    int i;

    for(i=0;i<H->TableSize;i++)
    {
        Position p=H->TheLists[i];
        Position temp;

        while(p!=NULL)
        {
            temp=p->next;
            free(p);
            p=temp;
        }
    }
    free(H->TheLists);
    free(H);
}
void printHash(HashTable H,int len)
{
    int i;
    for(i=0;i<len;i++)
    {
        Position p=H->TheLists[i];
        while(p)
        {
            printf("%d ",p->element);
            p=p->next;
        }
    }

}
int main()
{
   
    HashTable H;
    int array[]={17,60,29,38,1,2,3,4,60,13};
    int len=sizeof(array)/sizeof(array[0]);
    int i;


    H=InitializeTable(10);
    for(i=0;i<len;i++)
    {
        Insert(array[i],H);   
    }
    printHash( H,len);

    return 0;
}


搜索更多相关主题的帖子: 拉链 
2011-08-16 14:00
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
程序代码:
*Allocate table*/
    H=(HashTable)malloc(sizeof(struct HashTbl));
    if(NULL==H)
        printf("Out of space!!!\n");

    H->TableSize=NextPrime(TableSize);
    H->TheLists=(List *)malloc(sizeof(List)*H->TableSize);
    if(NULL==H->TheLists)
    {
      printf("Out of space!!!\n");
      exit(-1);
    }
    /*Allocate list  headers*/
    for(i=0;i<H->TableSize;i++)
    {
        H->TheLists[i]=(Position)malloc(sizeof(struct ListNode));
        if(H->TheLists[i]==NULL)
            printf("Out of space!!!\n");
        else
            H->TheLists[i]->next=NULL;
    }
    return H;

2011-08-16 14:58
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
程序代码:
/*
*哈希表 拉链法
*/
#include<stdio.h>
#include<stdlib.h>

#define MinTableSize 10

typedef int ElemType;
typedef unsigned int Index;

typedef struct ListNode
{
    ElemType element;
    struct    ListNode *next;
}*Position;

typedef Position List;

typedef struct HashTbl
{
    int TableSize;
     List *TheLists;
}*HashTable;

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!\n");
        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 lists*/
    H->TheLists=(List *)malloc(sizeof(List)*H->TableSize);
    if(NULL==H->TheLists)
    {
      printf("Out of space!!!\n");
      free(H);
      return NULL;
    }
    /*Allocate list  headers*/
    for(i=0;i<H->TableSize;i++)
    {
        H->TheLists[i]=(Position)malloc(sizeof(struct ListNode));
        if(H->TheLists[i]==NULL)
          printf("Out of space!!!\n");
   
           
        else
            H->TheLists[i]->next=NULL;
    }
    return H;
}

Position Find(ElemType Key,HashTable H)
{
    Position p;
    List L;

    L=H->TheLists[Hash(Key,H->TableSize)];
    p=L->next;
    while(p!=NULL&&p->element!=Key)/*Probably need strcmp!!*/
        p=p->next;

    return p;
}

void Insert(ElemType Key,HashTable H)
{
    Position pos,newCell;
    List L;

    pos=Find(Key,H);
    if(NULL==pos)/*Key is not found*/
    {
        newCell=(Position)malloc(sizeof(struct ListNode));
        if(NULL==newCell)
          printf("Out of space!!!");
       
           
        else
        {
            L=H->TheLists[Hash(Key,H->TableSize)];
            newCell->next=L->next;
            newCell->element=Key;/*Probably need strcpy*/
            L->next=newCell;
        }
    }
}

void DestroyTable(HashTable H)
{
    int i;

    for(i=0;i<H->TableSize;i++)
    {
        Position p=H->TheLists[i];
        Position temp;

        while(p!=NULL)
        {
            temp=p->next;
            free(p);
            p=temp;
        }
    }
    free(H->TheLists);
    free(H);
}

void printHash(HashTable H,int len)
{
    int i;
    for(i=0;i<len;i++)
    {
        Position p=H->TheLists[i];
        while(p)
        {
            printf("%d ",p->element);
            p=p->next;
        }
    }

}
int main()
{
   
    HashTable H;
    Position p=NULL;
    int array[]={19,14,23,01,68,20,84,27,55,11,10,79};
    int len=sizeof(array)/sizeof(array[0]);
    int i,k;
            
    H=InitializeTable(len);
    for(i=0;i<len;i++)
    {
        Insert(array[i],H);   
    }
    printHash(H,len);
    printf("\n\n");
   
    printf("please input the value which need find:");
    scanf("%d",&k);
    p=Find(k,H);
    if(p)
      printf("%d",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;
}


2011-08-16 16:34
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
还有点小问题:打印出的链表 没有元素的地方是乱码的 应该怎么解决
2011-08-16 16:47
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
程序代码:
HashTable InitializeTable(int TableSize)
{
    HashTable H;
    int i;

    if(TableSize<MinTableSize)
    {
        printf("Table size too small!\n");
        return NULL;
    }
   
    /*Allocate table*/
    H=(HashTable)malloc(sizeof(struct HashTbl));
    if(NULL==H)
        printf("Out of space!!!\n");

    H->TableSize=NextPrime(TableSize);

    /*Allocate list  headers*/
    for(i=0;i<H->TableSize;i++)
    {
        H->TheLists[i]=(Position)malloc(sizeof(struct ListNode));
        if(H->TheLists[i]==NULL)
            printf("Out of space!!!\n");
        else
            H->TheLists[i]->next=NULL;
       
        H->TheLists[i]->element=0;//////////这里要初始化下
    }
    return H;
}


2011-08-16 18:59
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
done!!!
2011-08-16 18:59
快速回复:完了 主函数写不好了 求助
数据加载中...
 
   



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

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