title: how to add a word in the link list while also checking and updating the frequency of the word in c
body: <p>I'm trying to finish one of my assignments and I have some issues. I have to make a program that uses struct to create a link list in which I have to add words. If the word is already in the linked list then I just have to update the frequency.</p>
  
  <p>I already have this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  typedef struct words Words;
  struct words{
      char *word;
      int freq;
      Words *next;
  };
  
  /*
  Inserts a copy of newWord into the list, in lexicographical order. If newWord is already
  in the list, increment the freq member of the node. The function returns a pointer to
  the list.
  */
  Words *addWord(Words *headPtr, char* newWord){
  Words *current = headPtr;
  if(headPtr == NULL)
  {
      current-&gt;word = newWord;
      current-&gt;freq = 1;
  }
  else
  {
      while(current != NULL)
          if(strcmp(headPtr-&gt;word, newWord))
          {
              current-&gt;freq++;
              return headPtr;
          }
          else
          {
              current-&gt;word = newWord;
              current-&gt;freq = 1;
          }
  }
  return headPtr;
  }
  
  //prints the words in the list, along with the frequency of each word
  void printWords(Words *headPtr){
  
      while(headPtr != NULL)
      {
          printf("%s: %d", headPtr-&gt;word, headPtr-&gt;freq);
          headPtr = headPtr-&gt;next;
      }
  }
  
  //frees the entire list. Note: Words **headPtr since the headPtr NULL upon return
  void deleteList(Words **headPtr){
      Words *current = *headPtr;
      Words *next;
      while(current != NULL)
      {
          next = current-&gt;next;
          free(current);
          current = next;
      }
      *headPtr = NULL;
  }
  
  int main(){
      char word[20];
      Words *list = NULL;
      scanf("%s", word);
      while(!feof(stdin)){
          list = addWord(list, word);
          scanf("%s", word);
      }
      printWords(list);
      deleteList(&amp;list);
  }
  </code></pre>