EVAmax 2021-12-16 22:44 采纳率: 100%
浏览 119
已结题

散列表用开放定址法存入字符串

下面附上了全部代码,用以下文件构建工程,进行整数类型存入都没什么问题,可是要存入字符串就不行了,到了strcpy函数复制字符串到散列表中就没办法运行了,可能是Element的类型没写对,奈何太笨了,搞了两天了也不知道怎么改,希望有人可以指正,最好能给出具体的解决方法。

问题相关代码,请勿粘贴截图

hashquad.c

              #include "fatal.h"
        #include "hashquad.h"
        #include <stdlib.h>
        #include <string.h>
        #include "fashfunc.c"
        
        #define MinTableSize (10)

        enum KindOfEntry { Legitimate, Empty, Deleted };
        struct HashEntry
        {
            ElementType  Element[10];
            enum KindOfEntry Info;
        };

        typedef struct HashEntry Cell;

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

        /* Return next prime; assume N >= 10 */

        static 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 )
                        goto ContOuter;  /* Sorry about this! */
                return N;
              ContOuter: ;
            }
        }



/* START: fig5_15.txt */
        HashTable
        InitializeTable( int TableSize )        //初始化散列表
        {
            HashTable H;
            int i,j;

/* 1*/      if( TableSize < MinTableSize )
            {
/* 2*/          Error( "Table size too small" );
/* 3*/          return NULL;
            }

            /* Allocate table */
/* 4*/      H = malloc( sizeof( struct HashTbl ) );
/* 5*/      if( H == NULL )
/* 6*/          FatalError( "Out of space!!!" );

/* 7*/      H->TableSize = NextPrime( TableSize );

            /* Allocate array of Cells */
/* 8*/      H->TheCells = malloc( sizeof( Cell ) * H->TableSize );
/* 9*/      if( H->TheCells == NULL )
/*10*/          FatalError( "Out of space!!!" );

/*11*/      for( i = 0; i < H->TableSize; i++ )
/*12*/      { 
                    H->TheCells[ i ].Info = Empty;
            }            

/*13*/      return H;
        }
/* END */

/* START: fig5_16.txt */
        Position
        Find( ElementType *Key, HashTable H )
        {
            Position CurrentPos;
            int CollisionNum;

/* 1*/      CollisionNum = 0;
/* 2*/      CurrentPos = Hash1( Key, H->TableSize );
/* 3*/      while( H->TheCells[ CurrentPos ].Info != Empty&&
            strcmp(H->TheCells[ CurrentPos ].Element,Key)!=0 )
                            /* Probably need strcmp!! */
            {    
                printf("%s\t第%d次插入不成功---位置%d\n",Key,CollisionNum+1,CurrentPos);
/* 4*/          CurrentPos += 2 * ++CollisionNum - 1;
/* 5*/          if( CurrentPos >= H->TableSize )
/* 6*/              CurrentPos -= H->TableSize;
            }
/* 7*/      return CurrentPos;
        }
/* END */

/* START: fig5_17.txt */
        void
        Insert( ElementType *Key, HashTable H )
        {
            Position Pos;

            Pos = Find( Key, H );
            if( H->TheCells[ Pos ].Info != Legitimate )
            {
                /* OK to insert here */
                H->TheCells[ Pos ].Info = Legitimate;
                strcpy( Key , H->TheCells[ Pos ].Element );
                         /* Probably need strcpy! */
                printf("%s\t插入位置%d\n",H->TheCells[Pos].Element,Pos);
            }
        }
/* END */


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

fashfunc.c

/* Here are some of the hash functions */
/* for strings that are in the text */

typedef unsigned int Index;

/* START: fig5_3.txt */
        Index
        Hash1( const char *Key, int TableSize )
        {
            unsigned int HashVal = 0;

/* 1*/      while( *Key != '\0' )
/* 2*/          HashVal += *Key++;

/* 3*/      return HashVal % TableSize;
        }
/* END */

/* START: fig5_4.txt */
        Index
        Hash2( const char *Key, int TableSize )
        {
            return ( Key[ 0 ] + 27 * Key[ 1 ] + 729 * Key[ 2 ] )
                        % TableSize;
        }
/* END */

/* START: fig5_5.txt */
        Index
        Hash3( const char *Key, int TableSize )
        {
            unsigned int HashVal = 0;

/* 1*/      while( *Key != '\0' )
/* 2*/          HashVal = ( HashVal << 5 ) + *Key++;

/* 3*/      return HashVal % TableSize;
        }
/* END */


testhash.c

#define QuadProb    /* Define the appropriate hash algorithm */


#ifdef QuadProb
    #include "hashquad.h"
#endif


#include <stdio.h>

#define NumItems 400
char *MyBirds[13]={ "robin", "sparrow", "hawk", "eagle", "seagull", "bluejay", "owl", "cardinal", "Jakana", "Moa", "Egret", "Penguin", "hawk" };
main( )
{
    HashTable H;
 //   Position P;
    int i;
    int j = 0;
    int CurrentSize;


    for(i=0;i<13;i++)
    {
        printf("%s\n",MyBirds[i]);
    }
    
    

    H = InitializeTable( CurrentSize = 26 );

    for(i=0;i<13;i++)
        Insert(MyBirds[i],H);
    printf("\n插入完毕\n");
    return 0;
}


hashquad.h

/* Interface for quadratic probing hash table */
typedef char ElementType;

/* START: fig5_14.txt */
        #ifndef _HashQuad_H
        #define _HashQuad_H

        typedef unsigned int Index;
        typedef Index Position;

        struct HashTbl;
        typedef struct HashTbl *HashTable;

        HashTable InitializeTable( int TableSize );
        void DestroyTable( HashTable H );
        Position Find( ElementType *Key, HashTable H );
        void Insert( ElementType *Key, HashTable H );
        ElementType Retrieve( Position P, HashTable H );
        HashTable Rehash( HashTable H );
        /* Routines such as Delete are MakeEmpty are omitted */

        #endif  /* _HashQuad_H */

/* END */


fatal.h


#include <stdio.h>
#include <stdlib.h>

#define Error( Str )        FatalError( Str )
#define FatalError( Str )   fprintf( stderr, "%s\n", Str ), exit( 1 )

  • 写回答

2条回答 默认 最新

  • 英雄哪里出来 2021年博客之星Top1 2021-12-17 19:13
    关注

    C++ 的话,字符串可以用 string
    C语言,只能 char * 加动态申请内存了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月17日
  • 已采纳回答 12月17日
  • 修改了问题 12月17日
  • 修改了问题 12月17日
  • 展开全部

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题