動態數組實現hash表

#include
#define HASHSIZE 12
#define NULL_KEY -111
typedef struct {
   int *arr;
   int size;
}Hashmap;


int initHash(Hashmap *hashmap);
void print(Hashmap *hashmap);


int main(void) {
  #Hashmap * hashmap=(Hashmap *)malloc(sizeof(Hashmap));

  #int flag;

  Hashmap * hashmap=NULL;   #malloc函數分配內存失敗返回NULL指針
  int flag;
  hashmap=(Hashmap *)malloc(sizeof(Hashmap));
  if(NULL==hashmap) {
     exit (1) ;
  }

  flag=initHash(hashmap);
  if(flag == 0)
      print(hashmap);
  free(hashmap);
  hashmap=NULL;
  return 0;
}


int initHash(Hashmap *hashmap) {
  int i;
  hashmap->size=HASHSIZE;
  hashmap->arr=(int *)malloc(HASHSIZE*sizeof(int));
  for(i=0;i<HASHSIZE;++i) {
      *(hashmap->arr+i)=NULL_KEY;
  }
  return 0;
}


void print(Hashmap *hashmap) {
   int i;
   for(i=0;i<HASHSIZE;++i) {
       printf ("%d\t",*(hashmap->arr+i));
   }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章