Thread: hashing problem

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    14

    hashing problem

    hI I use these functions for hashing ..

    Code:
    
    
    int hash(char *key) 
    { 
    	unsigned int hash_val = 0; 
    
    	while(*key != '\0') 
    		hash_val += *(key++); 
    
    	return(hash_val % HASHSIZE); 
    } 
    
    
    int hash_insert (char *key,int key_value) 
    { 
    	listNode *newptr,*curr,*ptr; 
    
    	int pos;
        pos = hash(key);
    
    	/* make sure it's not already in the hash table */
    	curr = HashTable[pos];
    	while(curr != NULL) {
                   
    		if (!strcmp(curr->code_name, key)) 
    			{curr->total_points=curr->total_points+key_value;
               
                }
    		curr = curr->next;
    	}
    
    
       ptr = HashTable[pos];
    	newptr = (struct listNode *)malloc(sizeof(listNode)); 
    	newptr->code_name = (char *)calloc( strlen( key) + 1, sizeof( char ) );
        strcpy(newptr->code_name,key); /*or newptr>code_name=key;*/
        newptr->total_points=key_value;
        newptr->next = NULL; 
    
    	if (HashTable[pos] == NULL) 
    	
    		{
         HashTable[pos] = newptr;
            } 
    	else  
        {
        while(ptr != NULL)
         {
    		ptr = ptr->next;
    	}
        ptr->next = newptr;
        
    	} 
    	return 1; 
    } 
    
    
    char* hash_find(char* key)
    {
    
    	int pos;
    
    	pos = hash(key);
    	
    	while(HashTable[pos] != NULL) {
    		if (!strcmp(HashTable[pos]->code_name, key)) 
    		{	printf("\nCodename:%s\nTotal Points:%d\n",HashTable[pos]->code_name ,HashTable[pos]->total_points);
                return key;}
                else{
    		HashTable[pos] = HashTable[pos]->next;} 
    	}
        
    	return NULL;
    }

    The probles is that when I store the data from a file i get a error message from windows . any help?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    while(ptr != NULL)
    {
       ptr = ptr->next;
    }
     //we got here when ptr is NULL
    ptr->next = newptr; //and here we got a GPF
    2 . /* make sure it's not already in the hash table */
    But you still continue your work even if the string is found
    3. Why do you need to insert at the end of the list? You can just always insert at the beginning
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    Code:
    int hash_insert (char *key,int key_value) 
    { 
    	listNode *newptr,*curr,*ptr; 
    
    	int pos;
        pos = hash(key);
    
    	/* make sure it's not already in the hash table */
    	curr = HashTable[pos];
    	while(curr != NULL) {
                   
    		if (!strcmp(curr->code_name, key)) 
    			{curr->total_points=curr->total_points+key_value;
               
              return 1;  }
    		curr = curr->next;
    	}
    
    
       ptr = HashTable[pos];
    	newptr = (struct listNode *)malloc(sizeof(listNode)); 
    	newptr->code_name = (char *)calloc( strlen( key) + 1, sizeof( char ) );
        strcpy(newptr->code_name,key); /*or newptr>code_name=key;*/
        newptr->total_points=key_value;
        newptr->next = NULL; 
    
    	if (HashTable[pos] == NULL) 
    	
    		{
         HashTable[pos] = newptr;
            } 
    	else  
        {
     
         
    	ptr = HashTable[pos];
    	HashTable[pos]=ptr;
        HashTable[pos]->next = ptr;
        	} 
    	return 1; 
    }

    This might be better but still the same prob. Is that has something to do with
    the Hsh table?

    listNode *HashTable[HASHSIZE];

    "The problem is when I reduce the HASHSIZE to 10. When I leave it to 997 for example nothing
    goes wrong.
    Last edited by kopros; 12-15-2006 at 11:24 AM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    HashTable[pos]=ptr;
    HashTable[pos]->next = ptr;
    Don't you see a problem?

    This
    Code:
    if (HashTable[pos] == NULL) 
    	
    		{
         HashTable[pos] = newptr;
            } 
    	else  
        {
     
         
    	ptr = HashTable[pos];
    	HashTable[pos]=ptr;
        HashTable[pos]->next = ptr;
        	}
    Could be replaced by
    newptr->next = HashTable[pos] ;
    HashTable[pos] = newptr;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    Thanks a lot friend you are the greatest... Any idea of how to sort the hash table by total points?
    qsort? mergesort?
    Im new in c. Sorry if my question look stupid...

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well why do u need to sort a hash???

    ssharish2005

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    You can't sort a hash. You can however, extract all the keys or values as an array and sort those.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM