示例#1
0
文件: htops.c 项目: shivin9/dsa
hash_item* populateHashtable(char filename[])
{
    FILE *fp = fopen(file, "r");
    printf("file opened!!\n");
    if(fp == NULL) {
        printf("invalid file name\n");
        exit(0);
    }
    int cost, id, i = 0;
    char code[6];
    char ch;
    int lines = 0;
    while(!feof(fp))
    {
        ch = fgetc(fp);
        if(ch == '\n')
        {
            lines++;
        }
    }
    rewind(fp);
    while(i < lines) {
        fscanf(fp, "%d", &id);
        fscanf(fp, "%s", code);
        fscanf(fp, "%d", &cost);
        i++;
        insertHashtable(ht, 10, id, code,cost);
    }
    fclose(fp);
}
示例#2
0
    Hash_item * populateHashtable( char * filename){
        Hash_item* table1=(Hash_item*) malloc(sizeof(Hash_id)*T_SIZE);
        memset(table1->table,0,sizeof(table1->table));
        FILE* fp= fopen(filename,"r");
        int cid,price;
        char item[20];
        while(fscanf(fp,"%d %s %d",&cid,item,&price)!=EOF){
            insertHashtable(table1,T_SIZE,cid,item,price);
        }
        return table1;
}
示例#3
0
文件: htops.c 项目: manitgupta/Codes
void populateHashtable(char filename[])
{
	FILE *fp;
	fp = fopen(filename,"r+");
	int cust_id,cost_item;
	char *item_code = (char*)malloc(sizeof(char)*10);
	while(!feof(fp))
	{	
		
		fscanf(fp, "%d %s %d",&cust_id,item_code,&cost_item);
		//printf("%d %s %d\n",cust_id,item_code,cost_item);
		//no error till here.
		insertHashtable(SIZE,cust_id,item_code,cost_item);
	}
	fclose(fp);
}
示例#4
0
文件: Hashtable.c 项目: weijunbao/Hdb
static int reHashTable(Hashtable* hashTable){
	int i;
	HashElement** _oldElements=NULL;
	if(hashTable->size == hashTable->capacity){
		int oldCapacity=hashTable->capacity;
		int newCapacity = hashTable->capacity * 2;
		if(newCapacity == 0)
			newCapacity = hashTable->factor;
		_oldElements = hashTable->elements;
		hashTable->elements = (HashElement**)malloc(sizeof(HashElement*) * newCapacity);
		if(!hashTable->elements){
			hashTable->elements = _oldElements;
			return 0;
		}
		memset(hashTable->elements,'\0',sizeof(HashElement*) * newCapacity);
		hashTable->capacity = newCapacity;
		hashTable->size = 0;
		for(i=0;i<oldCapacity;i++){
			HashElement* element = _oldElements[i];
			while(element){
				HashElement* tempElement;
				insertHashtable(hashTable,element->key,element->value);
				tempElement=element;
				element = element->next;
				free(tempElement);
			}
		}
		
		if(hashTable->freeFunc){
			
		}
		else{
			free(_oldElements);
		}
	}
	return 1;
}