title: Symbol table implementation using hash table in C
body: <p>I am trying to implement a simple symbol table that stores the strings in a hash table according to their hash values. The hash table in my program is an array of pointers to linked lists. we have 6 linked lists corresponding to each hash value.</p>
  
  <p>The problem is that though the program runs, it replaces the old strings with the new string in each iteration. </p>
  
  <p>my code is..</p>
  
  <pre><code>struct node{
      char *string;
      struct node *next;
  };
  struct node *hashtable[6];
  
  int calchash(char *arr);
  main()
      {
      char *line, a='n';
      int val, i;
              do{
              printf("Enter string:\n");
              scanf("%s", line);
              struct node *current;
              struct node *q= (struct node*)malloc(sizeof(struct node));
              q-&gt;string = line;
              q-&gt;next = NULL;
              val= calchash(line);
              if(hashtable[val] == NULL)
                      {
                      hashtable[val] = q;
                      current =q;}
              else{
                      current-&gt;next = q;
                      current = q;
                      }
              printf("Node created\n");
      for(i=0; i&lt;6; i++)
              { printf("Hash value %d :\n", i);
              if(hashtable[i]==NULL)
                      {printf("No STRINGS!\n\n");}
              else
                      {struct node *t = hashtable[i];
                      while(t != NULL)
                              {printf("%s \n", t-&gt;string);
                              t = t-&gt;next;}
                      printf("\n\n");
                     }
              }
  
      printf("CONTINUE(y/n):\n");
      scanf(" %c", &amp;a);
      }while(a!='n');
      }
  
  int calchash(char *arr)
      {int i=0, ascii;
      int sum=0;
      while(arr[i] != '\0')
              {ascii = arr[i];
              if(ascii&gt;=48 &amp;&amp; ascii&lt;=57)
                      {
                      sum+= 2*ascii;}
              else
                      {sum=sum+ ascii;}
              i++;
              }
      return ((sum*17+5)%6);
      }
  </code></pre>
  
  <p>And the output is:
  Enter string:
  az9</p>
  
  <p>Node created</p>
  
  <p>Hash value 0 :
  No STRINGS!</p>
  
  <p>Hash value 1 :
  No STRINGS!</p>
  
  <p>Hash value 2 :
  az9 </p>
  
  <p>Hash value 3 :
  No STRINGS!</p>
  
  <p>Hash value 4 :
  No STRINGS!</p>
  
  <p>Hash value 5 :
  No STRINGS!</p>
  
  <p>CONTINUE(y/n):
  y</p>
  
  <p>Enter string:
  Az9</p>
  
  <p>Node created</p>
  
  <p>Hash value 0 :
  No STRINGS!</p>
  
  <p>Hash value 1 :
  No STRINGS!</p>
  
  <p>Hash value 2 :
  Az9 </p>
  
  <p>Hash value 3 :
  No STRINGS!</p>
  
  <p>Hash value 4 :
  Az9 </p>
  
  <p>Hash value 5 :
  No STRINGS!</p>
  
  <p>CONTINUE(y/n):
  n</p>
  
  <p>Can someone please tell me what changes are needed so as to retain the previous az9 string under hash value 2???</p>