HashTable help?

This is a discussion on HashTable help? within the C Programming forums, part of the General Programming Boards category; so I should add it to the front of the list and , in other words add to temp? Code: ...

  1. #61
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so I should add it to the front of the list and , in other words add to temp?

    Code:
    boolean HashTableInsert(HashTable *this, const char* key, const char* value)
    {
      list_t *temp;
    
      int_u4 hashval = StringHash(key) % this->length;
    
      /* Does item already exist? */
      for (temp = this->table[hashval]; temp != NULL; temp = temp->next){
        if (strcmp(key, temp->key) == 0){
           StringDestruct(temp->value);
           temp->value = StringConstruct(value);
           return false;
        }
      }
      list_t *new_list;
      if ((new_list = malloc(sizeof(list_t))) == NULL)
        outOfMemory_(__LINE__, __FILE__, "struct node creation");
      new_list->key = StringConstruct(key);
      new_list->value = StringConstruct(value);
      new_list->next = NULL;
      temp->next = new_list;
      this->length = this->length + 1;
      return true;
    }
    I am not sure if I missed temp by one slot..
    Last edited by -EquinoX-; 03-29-2008 at 12:13 PM.

  2. #62
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    instead of

    new_list->next = NULL;
    temp->next = new_list;


    you need

    new_list->next = this->table[hashval];
    this->table[hashval] = new_list;


    And I do not understand what you intend by this->length = this->length + 1;?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #63
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    length is just recording the number of key,value pairs in the list.. as we are adding it then we add 1.. and after I add that it's perfect right?

  4. #64
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by vart View Post
    instead of

    new_list->next = NULL;
    temp->next = new_list;


    you need

    new_list->next = this->table[hashval];
    this->table[hashval] = new_list;


    And I do not understand what you intend by this->length = this->length + 1;?
    can you help me to understand why new_list-> next doesn't point to null instead it points to the head of the linked list?? I am kind of confused with this

  5. #65
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    can you help me to understand why new_list-> next doesn't point to null instead it points to the head of the linked list?? I am kind of confused with this
    There are 2 ways to enter new node into the list - as a last node or as a first node...

    suppose you have a list
    Code:
    head
      |
      V
      A -> B -> C -> NULL
    Write a code that adds the node D at the end of the list

    and another code - that adds the node at the beginning of the list

    Think - which code is faster

    Think - if there is difference for you what is the resulting order of the list.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #66
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I am getting more understanding here, so the code:

    new_list->next = this->table[hashval];
    this->table[hashval] = new_list;

    tries to insert it at the front of the list instead at the end of the list because it's faster that way, and we don't have to find where the end of the list is...
    we already know where the front of the list by the hashval.. am I right?
    Last edited by -EquinoX-; 03-30-2008 at 03:49 PM.

  7. #67
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    am I right?
    Yes. Your list is unordered - so the place where you put the new node is not important - choose the quickest way...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 5 of 5 FirstFirst 12345
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hashtable v.s. Dictionary
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-06-2008, 03:09 AM
  2. Missing Entries in Hashtable
    By wuzzo87 in forum C Programming
    Replies: 3
    Last Post: 05-13-2007, 01:46 AM
  3. Replies: 12
    Last Post: 10-22-2006, 09:37 PM
  4. game hashtable
    By disks86 in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2005, 01:42 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21