title: C - Separate Chaining Hash Table - Output and Space Issues
body: <p>so basically I wrote a program to initialize, insert, and output the whole hash table. I thought I did pretty good, but there's many issues.</p>
  <p>First issue being, some names are displayed with an additional weird character, why??</p>
  <p>Second issue being, I can only input a size parameter (for initialize(size) function) of &lt;8. Anything above 7 will output &quot;Out of Space!&quot; but why?? I thought I managed the space pretty well from what I was taught at uni:((</p>
  <p>Please help!</p>
  <pre><code>#include &lt;limits.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  typedef struct list_node *node_ptr;
  
  struct list_node
  {
      node_ptr next;
      char *key;
      char *value;
      
  };
  
  typedef node_ptr LIST;
  typedef node_ptr position;
  
  struct hash_table
  {
      LIST *list_ptr_arr;
      unsigned int table_size;
  };
  
  typedef struct hash_table *HASHTABLE;
  
  unsigned long long int
  hash(const char *key, unsigned int hash_size)
  {
  
      unsigned long long int hash;
  
      for(int i = 0; key[i]; i++)
      {
          hash = (hash&lt;&lt;32)+key[i];
      }
  
      return (hash%hash_size);
  
  }
  
  unsigned int 
  next_prime(int number)
  {
  
      int j;
  
      for(int i = number; ; i++)
      {
          for(j = 2; j&lt;i; j++)
          {
              if(i%j == 0){break;}
          }
  
          if(i==j){return j;}
      }
  }
  
  HASHTABLE
  initialize(unsigned int table_size)
  {
      HASHTABLE H;
  
      H = (HASHTABLE) malloc(sizeof(struct hash_table));
      if(H==NULL){printf(&quot;Out of Space!&quot;); return 0;}
  
      H-&gt;table_size = next_prime(table_size);
  
      H-&gt;list_ptr_arr = (position*) malloc(sizeof(LIST)*H-&gt;table_size);
      if(H-&gt;list_ptr_arr==NULL){printf(&quot;Out of Space!&quot;); return 0;}
  
      H-&gt;list_ptr_arr = (LIST*) malloc(sizeof(struct list_node)*H-&gt;table_size);
  
      for(unsigned int i = 0; i&lt;H-&gt;table_size; i++)
      {
          if(H-&gt;list_ptr_arr[i]==NULL){printf(&quot;Out of Space!&quot;); return 0;}
  
          H-&gt;list_ptr_arr[i]=NULL;
      }
  
  
      return H;
      
  }
  
  position
  set(const char *key, const char *value)
  {
      position entry = (position) malloc(sizeof(struct list_node));
      entry-&gt;value = (char*) malloc(strlen(value)+1);
      entry-&gt;key = (char*) malloc(strlen(key)+1);
      strncpy(entry-&gt;key,key,strlen(key));
      strncpy(entry-&gt;value,value,strlen(value));
      entry-&gt;next = NULL;
  
      return entry;
  }
  
  void
  insert(const char *key, const char *value, HASHTABLE H)
  {
      unsigned int slot = hash(key, H-&gt;table_size);
      node_ptr entry = H-&gt;list_ptr_arr[slot];
  
      node_ptr prev;
  
      if(entry==NULL)
      {
          
          H-&gt;list_ptr_arr[slot] = set(key, value);
          return;
      }
  
      while(entry!=NULL)
      {
          if(strcmp(entry-&gt;key, key)==0)
          {
              free(entry-&gt;value);
              entry-&gt;value = malloc(strlen(value)+1);
              strncpy(entry-&gt;value,value,strlen(value));
              return;
          }
  
          prev = entry;
          entry = prev-&gt;next;
  
      }
  
      
      prev-&gt;next = set(key, value);
  
  }
  
  void
  dump(HASHTABLE H)
  {
  
      for(unsigned int i = 0; i&lt;H-&gt;table_size; i++)
      {
          position entry = H-&gt;list_ptr_arr[i];
  
          if(H-&gt;list_ptr_arr[i]==NULL){continue;}
  
          printf(&quot;slot[%d]: &quot;, i);
  
          for(;;)
          {
              printf(&quot;%s|%s -&gt; &quot;, entry-&gt;key, entry-&gt;value);
  
              if(entry-&gt;next == NULL)
              {
                  printf(&quot;NULL&quot;);
                  break;
              }
  
              entry = entry-&gt;next;
          }
  
          printf(&quot;\n&quot;);
  
      }
  
  }
  
  
  int main()
  {
      
      HASHTABLE H = initialize(7);
      insert(&quot;name1&quot;, &quot;David&quot;, H);
      insert(&quot;name2&quot;, &quot;Lara&quot;, H);
      insert(&quot;name3&quot;, &quot;Slavka&quot;, H);
      insert(&quot;name4&quot;, &quot;Ivo&quot;, H);
      insert(&quot;name5&quot;, &quot;Radka&quot;, H);
      insert(&quot;name6&quot;, &quot;Kvetka&quot;, H);
      dump(H);
    
      return 0;   
      
  }
  </code></pre>
  <p>Then I tried to change it up a bit:</p>
  <pre><code>#include &lt;limits.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  typedef struct list_node *node_ptr;
  
  struct list_node
  {
      node_ptr next;
      char *key;
      char *value;
      
  };
  
  typedef node_ptr LIST;
  typedef node_ptr position;
  
  struct hash_table
  {
      LIST *list_ptr_arr;
      unsigned int table_size;
  };
  
  typedef struct hash_table *HASHTABLE;
  
  unsigned long long int
  hash(const char *key, unsigned int hash_size)
  {
  
      unsigned long long int hash;
  
      for(int i = 0; key[i]; i++)
      {
          hash = (hash&lt;&lt;32)+key[i];
      }
  
      return (hash%hash_size);
  
  }
  
  unsigned int 
  next_prime(int number)
  {
  
      int j;
  
      for(int i = number; ; i++)
      {
          for(j = 2; j&lt;i; j++)
          {
              if(i%j == 0){break;}
          }
  
          if(i==j){return j;}
      }
  }
  
  HASHTABLE
  initialize(unsigned int table_size)
  {
      HASHTABLE H;
  
      H = (HASHTABLE) malloc(sizeof(struct hash_table));
      if(H==NULL){printf(&quot;Out of Space!1&quot;); return 0;}
  
      H-&gt;table_size = next_prime(table_size);
  
      H-&gt;list_ptr_arr = (position*) malloc(sizeof(LIST)*H-&gt;table_size);
      if(H-&gt;list_ptr_arr==NULL){printf(&quot;Out of Space!2&quot;); return 0;}
  
      H-&gt;list_ptr_arr = (LIST*) malloc(sizeof(struct list_node)*H-&gt;table_size);
  
      for(unsigned int i = 0; i&lt;H-&gt;table_size; ++i)
      {
          if(H-&gt;list_ptr_arr[i]==NULL){printf(&quot;Out of Space!3&quot;); return 0;}
  
          H-&gt;list_ptr_arr[i]-&gt;value=&quot;HEAD&quot;;
          H-&gt;list_ptr_arr[i]-&gt;next=NULL;
      }
  
  
      return H;
      
  }
  
  
  void
  insert(const char *key, const char *value, HASHTABLE H)
  {
      unsigned int slot = hash(key, H-&gt;table_size);
      LIST entry = H-&gt;list_ptr_arr[slot], newNode;
  
  
  
     
  
      newNode = (position) malloc(sizeof(struct list_node));
      if(newNode==NULL){printf(&quot;Out of Space4!&quot;); return;}
      newNode-&gt;next = entry-&gt;next;
      strncpy(newNode-&gt;key,key,strlen(key));
      strncpy(newNode-&gt;value,value,strlen(value));
      entry-&gt;next = newNode;
  
  }
  
  void
  dump(HASHTABLE H)
  {
  
      for(unsigned int i = 0; i&lt;H-&gt;table_size; i++)
      {
          position entry = H-&gt;list_ptr_arr[i];
          position p = entry-&gt;next;
  
          if(p==NULL){continue;}
  
          printf(&quot;slot[%d]: &quot;, i);
  
          for(;;)
          {
              printf(&quot;%s|%s -&gt; &quot;, p-&gt;key, p-&gt;value);
  
              if(p-&gt;next == NULL)
              {
                  printf(&quot;NULL&quot;);
                  break;
              }
  
              p = p-&gt;next;
          }
  
          printf(&quot;\n&quot;);
  
      }
  
  }
  
  
  int main()
  {
      
      HASHTABLE H = initialize(4);
      insert(&quot;name1&quot;, &quot;David&quot;, H);
      insert(&quot;name2&quot;, &quot;Lara&quot;, H);
      insert(&quot;name3&quot;, &quot;Slavka&quot;, H);
      insert(&quot;name4&quot;, &quot;Ivo&quot;, H);
      insert(&quot;name5&quot;, &quot;Radka&quot;, H);
      insert(&quot;name6&quot;, &quot;Kvetka&quot;, H);
      dump(H);
    
      return 0;   
      
  }
  
     
  </code></pre>
  <p>Thank you!</p>