int main()
{
	int len = 15;	//哈希表长,亦即表中槽的数目
	printf("We set the length of hashtable %d\n",len);

	//创建哈希表并插入数据
	HashTable hashtable = create_HashTable(len);
	if(insert_HashTable(hashtable,1))
		printf("insert 1 success\n");
	else 
		printf("insert 1 fail,it is already existed in the hashtable\n");
	if(insert_HashTable(hashtable,8))
		printf("insert 8 success\n");
	else 
		printf("insert 8 fail,it is already existed in the hashtable\n");
	if(insert_HashTable(hashtable,3))
		printf("insert 3 success\n");
	else 
		printf("insert 3 fail,it is already existed in the hashtable\n");
	if(insert_HashTable(hashtable,10))
		printf("insert 10 success\n");
	else 
		printf("insert 10 fail,it is already existed in the hashtable\n");
	if(insert_HashTable(hashtable,8))
		printf("insert 8 success\n");
	else 
		printf("insert 8 fail,it is already existed in the hashtable\n");

	//查找数据
	pNode pFind1 = search_HashTable(hashtable,10);
	if(pFind1)
		printf("find %d in the hashtable\n",pFind1->data);		
	else 
		printf("not find 10 in the hashtable\n");
	pNode pFind2 = search_HashTable(hashtable,4);
	if(pFind2)
		printf("find %d in the hashtable\n",pFind2->data);		
	else 
		printf("not find 4 in the hashtable\n");

	//删除数据
	if(delete_HashTable(hashtable,1))
		printf("delete 1 success\n");
	else 
		printf("delete 1 fail");
	pNode pFind3 = search_HashTable(hashtable,1);
	if(pFind3)
		printf("find %d in the hashtable\n",pFind3->data);		
	else 
		printf("not find 1 in the hashtable,it has been deleted\n");

	//销毁哈希表
	destroy_HashTable(hashtable,len);
	return 0;
}
Example #2
0
static int
dbf_create(ClipMachine * ClipMachineMemory, RDD_DATA_VTBL * vtbl, char *name, RDD_STRUCT * stru, int nfields, const char *__PROC__)
{
   RDD_FILE file;

   DBF_HEADER *hdr = malloc(sizeof(DBF_HEADER));

   char *chdr, *s;

   DBF_FIELD *fld;

   int i, offs;

   time_t ltm;

   struct tm *date;

   HashTable *hashes;

   int er;

   unsigned int recsize;

   memset(hdr, 0, sizeof(DBF_HEADER));
   hdr->version = vtbl->dbfsig;
   for (i = 0; i < nfields; i++)
      if (strchr("MPG", stru[i].type) || (stru[i].type == 'V' && stru[i].len != 3 && stru[i].len != 4))
      {
	 hdr->version = vtbl->dbfwithmemosig;
	 break;
      }
   ltm = time(NULL);
   date = gmtime(&ltm);
   hdr->yy = date->tm_year;
   hdr->mm = date->tm_mon + 1;
   hdr->dd = date->tm_mday;
   _rdd_put_uint(hdr->recs, 0);
   _rdd_put_ushort(hdr->hdrsize, sizeof(DBF_HEADER) + nfields * sizeof(DBF_FIELD) + 2);
   recsize = 1;
   hdr = realloc(hdr, _rdd_ushort(hdr->hdrsize) + 1);
   hashes = new_HashTable();
   offs = 1;
   for (i = 0; i < nfields; i++)
   {
      recsize += stru[i].len;
      fld = (DBF_FIELD *) ((char *) hdr + sizeof(DBF_HEADER) + i * sizeof(DBF_FIELD));
      memset(fld, 0, sizeof(DBF_FIELD));
      strncpy(fld->name, stru[i].name, 10);
      for (s = fld->name; *s && *s != ' '; s++);
      *s = 0;
      loc_write(dbf_get_locale(ClipMachineMemory), (unsigned char *) (fld->name), strlen(fld->name));
      fld->type = stru[i].type;
      if (stru[i].type == 'V' && stru[i].len != 3 && stru[i].len != 4 && stru[i].len < 6)
      {
	 er = rdd_err(ClipMachineMemory, EG_CREATE, 0, __FILE__, __LINE__, __PROC__, er_badstructure);
	 goto err;
      }
      _rdd_put_uint(fld->offs, offs);
      offs += stru[i].len;
      if (fld->type == 'N')
      {
	 if (stru[i].dec >= stru[i].len - (stru[i].dec != 0))
	 {
	    er = rdd_err(ClipMachineMemory, EG_CREATE, 0, __FILE__, __LINE__, __PROC__, er_badstructure);
	    goto err;
	 }
	 fld->len.num.len = stru[i].len;
	 fld->len.num.dec = stru[i].dec;
      }
      else if (fld->type == 'X')
      {
	 if (stru[i].len < 10 || stru[i].len > 127)
	 {
	    er = rdd_err(ClipMachineMemory, EG_CREATE, 0, __FILE__, __LINE__, __PROC__, er_badstructure);
	    goto err;
	 }
	 fld->len.num.len = stru[i].len;
	 fld->len.num.dec = stru[i].dec;
      }
      else
      {
	 _rdd_put_ushort((unsigned char *) (fld->len.len), stru[i].len);
      }
      if (!HashTable_insert(hashes, fld, _clip_casehashword(fld->name, strlen(fld->name))))
      {
	 er = rdd_err(ClipMachineMemory, EG_CREATE, 0, __FILE__, __LINE__, __PROC__, er_fielduplicate);
	 goto err;
      }
   }
   if (recsize > 0xffff)
   {
      er = rdd_err(ClipMachineMemory, EG_CREATE, 0, __FILE__, __LINE__, __PROC__, er_badstructure);
      goto err;
   }
   _rdd_put_ushort(hdr->recsize, recsize);
   delete_HashTable(hashes);

   chdr = (char *) hdr;
   chdr[_rdd_ushort(hdr->hdrsize) - 2] = 0x0D;
   chdr[_rdd_ushort(hdr->hdrsize) - 1] = 0x00;
   chdr[_rdd_ushort(hdr->hdrsize)] = 0x1A;

   memset(&file, 0, sizeof(RDD_FILE));
   file.md = (char *) -1;
   file.fd = _clip_creat(ClipMachineMemory, name, O_RDWR, ClipMachineMemory->fileCreateMode, 1);
   if (file.fd == -1)
      goto err_create;
   if ((er = rdd_write(ClipMachineMemory, &file, 0, _rdd_ushort(hdr->hdrsize) + 1, (char *) hdr, __PROC__)))
      goto err;
   if (_clip_close(ClipMachineMemory, _clip_hashstr(name), file.fd) == -1)
      goto err_create;

   free(hdr);
   return 0;
 err_create:
   er = rdd_err(ClipMachineMemory, EG_CREATE, errno, __FILE__, __LINE__, __PROC__, name);
 err:
   free(hdr);
   return er;
}
Example #3
0
int main()
{
    HashTable t;

    int a=1, b=2, c=3;
    HashTableElement* E1;
    HashTableElement* E2;
    HashTableElement* E3;

    //size 10
    printf("bucket size is 1:\n");
    init_HashTable(&t, 100);

    set_HashTable(&t, "one", &a);
    set_HashTable(&t, "two", &b);
    set_HashTable(&t, "there", &c);

    E1=get_HashTable(&t, "one");
    E2=get_HashTable(&t, "two");
    E3=get_HashTable(&t, "there");
    if(E1!=NULL)
    {
        printf("%s:%d\n", E1->key, *(int*)(E1->valuePoint));
    }
    if(E2!=NULL)
    {
        printf("%s:%d\n", E2->key, *(int*)(E2->valuePoint));
    }
    if(E3!=NULL)
    {
        printf("%s:%d\n", E3->key, *(int*)(E3->valuePoint));
    }

    delete_HashTable(&t, "two");
    traverse_HashTable(&t, print_hash_element);

    set_HashTable(&t, "two", &b);
    traverse_HashTable(&t, print_hash_element);

    set_HashTable(&t, "two", &c);
    traverse_HashTable(&t, print_hash_element);

    clear_HashTable(&t);
    traverse_HashTable(&t, print_hash_element);

    destroy_HashTable(&t);

    //size 1
    printf("bucket size is 1:\n");
    init_HashTable(&t, 1);

    set_HashTable(&t, "one", &a);
    set_HashTable(&t, "two", &b);
    set_HashTable(&t, "there", &c);

    E1=get_HashTable(&t, "one");
    E2=get_HashTable(&t, "two");
    E3=get_HashTable(&t, "there");
    if(E1!=NULL)
    {
        printf("%s:%d\n", E1->key, *(int*)(E1->valuePoint));
    }
    if(E2!=NULL)
    {
        printf("%s:%d\n", E2->key, *(int*)(E2->valuePoint));
    }
    if(E3!=NULL)
    {
        printf("%s:%d\n", E3->key, *(int*)(E3->valuePoint));
    }

    delete_HashTable(&t, "two");
    traverse_HashTable(&t, print_hash_element);

    set_HashTable(&t, "two", &b);
    traverse_HashTable(&t, print_hash_element);

    set_HashTable(&t, "two", &c);
    traverse_HashTable(&t, print_hash_element);

    clear_HashTable(&t);
    traverse_HashTable(&t, print_hash_element);

    destroy_HashTable(&t);

    //size 0
    printf("bucket size is 0:\n");
    if(!init_HashTable(&t, 0))
    {
        printf("init error\n");
    }

    return 0;
}