一个HashTable操作的程序

  1: /*

 

   2:  * FilePath:    ./home/ijse/MyCodes/Hash/main.h

 

   3:  * Author:  ijse

 

   4:  * Email:   [email protected]

 

   5:  * Date:    2010-07-05

 

   6:  * Site:    http://www.ijser.cn

 

   7:  * All Rights Reserved 2010

 

   8:  */

 

   9:  

 

  10: #ifndef MAIN_H_INCLUDED

 

  11: #define MAIN_H_INCLUDED

 

  12:  

 

  13: #include <stdio.h>

 

  14: #include <malloc.h>

 

  15:  

 

  16: #define SUCCESS 1

 

  17: #define UNSUCCESS 0

 

  18: #define OK 1

 

  19: #define DUPLICATE -1

 

  20:  

 

  21: typedef  int ElemType;

 

  22: typedef int KeyType;

 

  23:  

 

  24: typedef struct {

 

  25:         ElemType *elem;

 

  26:         int count;

 

  27:         int sizeindex;

 

  28: } HashTable;

 

  29:  

 

  30:  

 

  31: extern int Hash(int K);

 

  32: extern int collision(HashTable H,int *p,int *c);

 

  33:  

 

  34: /*

 

  35:  * Print the HashTable's status

 

  36:  */

 

  37: void PrintHashTableStatus(HashTable H) {

 

  38:     int i=0;

 

  39:     printf("/n=======================================/n");

 

  40:     printf("Now the HashTable's status is:/n");

 

  41:     for(i=0;i<H.sizeindex;++i)

 

  42:         printf("/t%d",i);

 

  43:     printf("/n");

 

  44:     for(i=0;i<H.sizeindex;++i)

 

  45:         printf("/t%d",*(H.elem+i));

 

  46:     printf("/n");

 

  47:     printf("The count of HashTable's elements is %d",H.count);

 

  48:     printf("/n=======================================/n");

 

  49:     printf("Press [Enter] to continue...");

 

  50:  

 

  51:     getchar();

 

  52:  

 

  53: }

 

  54:  

 

  55:  

 

  56: /*

 

  57:  * Search and return p

 

  58:  * @param HashTable

 

  59:  * @param KeyType the keyword

 

  60:  * @param int* p to return the HashCode

 

  61:  * @param int* c conflict count

 

  62:  * @return int

 

  63:  */

 

  64: int SearchHash(HashTable H, KeyType K, int *p, int *c) {

 

  65:     *p = Hash(K);

 

  66:     /*printf("%d/n",K!=*(H.elem+*p));

 

  67:     printf("/nCollision Happen:p=%d,K=%d,h.elem=%d/n",*p,K,*(H.elem+*p));

 

  68:     printf("h.elem+*p == 0? %d/n",*(H.elem+*p)==0);

 

  69:     getchar();*/

 

  70:     printf("Got the HashCode %d, and the next step is to see whether there is a confiction, press [Enter] to continue.../n",*p);

 

  71:     getchar();

 

  72:     while(*(H.elem+*p)!=0 && K!=*(H.elem+*p)) { /* handle the confliction */

 

  73:         printf("/nCollision Happen:/n/tp=%d,K=%d,H.elem=%d/n",*p,K,*(H.elem+*p));

 

  74:         printf("Press [Enter] to handle the confliction.../n");

 

  75:         getchar();

 

  76:         collision(H,p,c);

 

  77:     }

 

  78:     printf("The HashCode create successful, it is %d/n",*p);

 

  79:     /*printf("/nSearchHash() return p=%d/n",*p);*/

 

  80:     if(K==*(H.elem+*p)) /* find the element */

 

  81:         return SUCCESS;

 

  82:     else /* didn't find the element */

 

  83:         return UNSUCCESS;

 

  84: }

 

  85:  

 

  86:  

 

  87: /*

 

  88:  * Insert Element into HashTable

 

  89:  * @param HashTable

 

  90:  * @param ElemType e

 

  91:  * @return int

 

  92:  */

 

  93: int InsertHash(HashTable *H, ElemType e) {

 

  94:     int c = 0;

 

  95:     int p = 0;

 

  96:     int i = 0;

 

  97:  

 

  98:     /* See the HashTable's status */

 

  99:     PrintHashTableStatus(*H);

 

 100:  

 

 101:     if(SearchHash(*H,e,&p,&c))

 

 102:         return DUPLICATE;

 

 103:     else {

 

 104:         printf("Now save %d into the HashTable. Press [Enter] to continue.../n",e);

 

 105:         *(H->elem+p) = e;

 

 106:         ++(H->count);

 

 107:         PrintHashTableStatus(*H);

 

 108:         return OK;

 

 109:     } /*

 

 110:     else {

 

 111:         RecreateHashTable(H);

 

 112:         printf("FUNCTION SEARCHHASH() RETURNED UNSUCCESS!!/n");

 

 113:         return UNSUCCESS;

 

 114:     }

 

 115:     */

 

 116: }

 

 117:  

 

 118: /*

 

 119:  * Delete element from HashTable

 

 120:  */

 

 121: int DeleteHashElem(HashTable *H, KeyType K, int *p, int *c) {

 

 122:     printf("Delete %d:/n",K);

 

 123:     PrintHashTableStatus(*H);

 

 124:     if(SearchHash(*H,K,p,c)==SUCCESS) { /* find the element, delete it */

 

 125:         printf("Find %d, press [Enter] to delete it!",K);

 

 126:         getchar();

 

 127:         *(H->elem+*p)=0;

 

 128:         printf("Delete successfully!/n");

 

 129:         return SUCCESS;

 

 130:     } else {

 

 131:         printf("Can't find %d, delete failed~!/n",K);

 

 132:         return UNSUCCESS;

 

 133:     }

 

 134:  

 

 135: }

 

 136:  

 

 137: /*

 

 138:  * Initialize the HashTable

 

 139:  * @param HashTable

 

 140:  * @param int datasize  how many elements

 

 141:  */

 

 142: void InitHashTable(HashTable *H,int datasize) {

 

 143:     /*H = (HashTable*)malloc(sizeof(HashTable*));*/

 

 144:     int i=0;

 

 145:     H->elem=(ElemType*)malloc(datasize*sizeof(ElemType*));

 

 146:  

 

 147:     /*!!!!There is no need in Linux GNU GCC !!!!!*/

 

 148:     for(i=0;i<datasize;++i) {

 

 149:         *(H->elem+i)=0;

 

 150:     }

 

 151:     /*********************************************/

 

 152:  

 

 153:     H->count=0;

 

 154:     H->sizeindex=datasize;

 

 155: }

 

 156:  

 

 157:  

 

 158: #endif /* MAIN_H_INCLUDED*/

你可能感兴趣的:(struct,function,delete,search,insert,conflict)