Thread: HashTable help?

  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 11:13 AM.

  2. #62
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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;?
    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. #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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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.
    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

  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 02:49 PM.

  7. #67
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    am I right?
    Yes. Your list is unordered - so the place where you put the new node is not important - choose the quickest way...
    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

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, 02:09 AM
  2. Missing Entries in Hashtable
    By wuzzo87 in forum C Programming
    Replies: 3
    Last Post: 05-13-2007, 12:46 AM
  3. Replies: 12
    Last Post: 10-22-2006, 08: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