title: Unable to delete value from HashTable in C
body: <p>I'm trying to delete a value from hash table, the code works when I delete a value from the array where the linked list size is one, then segfaults if the size is greater than 1.</p>
  
  <pre><code>typedef struct hash {
      char *key;
      char *value;
      struct hash *next;
  } hashTable;
  
  // generate hash code
  unsigned int hashCode(char *key) {
      int sum;
      int i;
  
      sum = 0;
      i = 0;
      while (key[i])
          sum += key[i++];
      return sum % MAX_HASH;
  }
  
  // get hash item size
  int hash_size(hashTable *head) {
      int i;
      hashTable *list;
  
      i = 0;
      list = head;
      if (list) {
          while (list != NULL) {
              i++;
              list = list-&gt;next;
          }
      }
      return (i);
  }
  
  // free item
  void free_hash(hashTable *item) {
      free(item-&gt;key);
      free(item-&gt;value);
      free(item);
  }
  
  // function for deleting item from hash table
  void deleteItem(hashTable *table[], char *key) {
      hashTable *head = table[hashCode(key)];
      hashTable *tmp = head;
      hashTable *prev = NULL;
  
      if (!head)
          return;
  
      if (hash_size(tmp) == 1) {
          table[hashCode(key)] = 0;
          free_hash(tmp);
          return;
      }
      while (strcmp(tmp-&gt;key, key) != 0 &amp;&amp; tmp-&gt;next != NULL) {
          prev = tmp;
          tmp = tmp-&gt;next;
      }
  
      if (strcmp(tmp-&gt;key, key) == 0) {
          if (prev)
              prev-&gt;next = tmp-&gt;next;
          else
              head = tmp-&gt;next;
          free_hash(tmp);
      }
  }
  
  // function for inserting item into the table
  void insert(hashTable *table[], char *key, char *value) {
      hashTable *tmp;
      hashTable *item;
      unsigned int code;
  
      item = (hashTable *)malloc(sizeof(hashTable));
      if (!item)
          return;
      item-&gt;key = (char *)malloc(sizeof(char) * strlen(key) + 1);
      item-&gt;value = (char *)malloc(sizeof(char) * strlen(value) + 1);
      item-&gt;next = NULL;
      code = hashCode(key);
      strcpy(item-&gt;key, key);
      strcpy(item-&gt;value, value);
      if (!table[code])
          table[code] = item;
      else {
          tmp = table[code];
          item-&gt;next = tmp;
          table[code] = item;
      }
  }
  
  // displaying items
  void display(hashTable *table[]) {
      int i = 0;
      hashTable *tmp;
  
      while (i &lt; MAX_HASH) {
          if (table[i] != NULL) {
              tmp = table[i];
              if (hash_size(tmp) == 1)
                  printf("%s=%s\n", tmp-&gt;key, tmp-&gt;value);
              else {
                  while (tmp != NULL) {
                      printf("%s=%s\n", tmp-&gt;key, tmp-&gt;value);
                      tmp = tmp-&gt;next;
                  }
              }
          }
          i++;
      }
  }
  
  int main(int argc, char const *argv[]) {
      hashTable *table[MAX_HASH];
  
      memset(table, 0, MAX_HASH * sizeof(hashTable *));
      insert(table, "Bart", "first");
      insert(table, "Lisa", "Second");
      insert(table, "Foo", "bar");
      deleteItem(table, "Lisa");
      display(table);
      return 0;
  }
  </code></pre>