Thread: Duplicate at Hash Table

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    54

    Duplicate at Hash Table

    Hi, I'm running in a little trouble here. I have a hash functions which hashes a string into a hash table that is linked via linked list. It seems to produce duplicates in my node. Here are pieces of the code :

    I have a fixed length record of 20 chars read from a file, I placed 21 because of the "\n". After running a strlen to count, my output is as follows :

    Code:
    Output:
    
    Length : 0 0
    Length : 21 0
    Length : 42 21
    Length : 42 21
    Seems that it manage to sneak another record into my first variable. I can't figure out what's wrong.

    Code:
    typedef struct Bucket
    {
      /* Extra node for chaining (Not relevant) */
      OverflowBucket* head;
    
      char record1[21];
      char record2[21];
    
      struct Bucket* next;
    }Bucket;
    
    /* Hash Table */
    typedef struct Hashtable
    {
      Bucket* head;
    }HashTable;
    
    
    /* This is something to simplify you guys to generate the table of 0,1,2 string keys*/
    HashTable* generateTable(HashTable* table)
    {
      int counter = 4;
    
      Bucket *bucket, *currBucket;
    
      while(counter != 0)
      {
    
        bucket = malloc(sizeof(Bucket));
        if(bucket == NULL)
        {
          fprintf(stderr,"Malloc bucket failed\n");
          exit(1);
        }
        bucket->next = NULL;
    
        if(table->head == NULL)
        {
          table->head = bucket;
          currBucket = bucket;
        }
        else
        {
          currBucket->next = bucket;
        }
        counter--;
      }
    
      return table;
    }
    
    int hash(char* buffer)
    {
      int h = 0, a = 127;
    
      for(;*buffer != '\0'; buffer++)
        h = (a*h + *buffer) % 3;
    
      return h;
    }
    
    void hashToTable(char* buffer,HashTable* table)
    {
      int key,count;
      Bucket *currBucket;
    
      currBucket = table->head;
    
      key = hash(buffer);
    
      count = key;
    
      /* if string key is 0 */
      if(key == 0)
      {
        if(strlen(currBucket->record1) == 0)
          strcpy(currBucket->record1,buffer);
        else if(strlen(currBucket->record2) == 0)
          strcpy(currBucket->record2,buffer);
      }
      else
      {
        /* Get corresponding node to the string key */
        while(currBucket->next != NULL)
        {
          if(count != 0)
          {
            count--;
            currBucket = currBucket->next;
          }
          else
          {
            if(strlen(currBucket->record1) == 0)
              strcpy(currBucket->record1,buffer);
            else if(strlen(currBucket->record2) == 0)
              strcpy(currBucket->record2,buffer);
          }
        }
      }
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    I'm still unable to figure out why did a duplicate copy entered my first record. Any ideas?

    This is my output in strings
    Code:
    Output :
    
    Record1 : 
    Timmy...............
    John................
    
    Record2 :
    John................
    Where it should only be

    Code:
    Output:
    
    Record1 : 
    Timmy...............
    
    Record2 :
    John................
    Need help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dictionary in C# to hash table in C?
    By dinoman in forum C Programming
    Replies: 2
    Last Post: 04-12-2009, 09:23 PM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. Group Project Help/Volunteer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 11:36 PM
  4. Hash table creation and insertion
    By tgshah in forum C Programming
    Replies: 1
    Last Post: 01-23-2006, 07:54 PM
  5. Not sure on hash table memory allocations
    By Thumper333 in forum C Programming
    Replies: 3
    Last Post: 09-27-2004, 09:00 PM