title: How to insert string into linked list in c
body: <p>I want to write a function which can be insert string alphabetically.
  It can be declared as;</p>
  
  <pre><code>typedef struct Node Node;
  typedef struct Node
  {
      char *data;
      Node *next;
  };
  
  Node *insertion(Node *head,char *arr);
  </code></pre>
  
  <p>How can I define this function?</p>
  
  <p>My codes;</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  typedef struct Node Node;
  typedef struct Node
  {
      char *data;
      Node *next;
  };
  Node *MyList = 0;
  void AddWord(char *word) // Add a new word to the list MyList
      { Node *item,*next;
      item = (Node *) malloc(sizeof(Node));
      if ( item==0 ) { printf ("Malloc Failed \n");  return ; }
      strcpy(item-&gt;data,word); // Copy Word into new Item
      item-&gt;next = 0; // Set that next Item is nothing
  
      if ( MyList == 0 ) // If List is Empty, make this the first item
          { MyList = item; return ; }
  
      if(strcmp(word,MyList-&gt;data) &lt; 0 ) // Check if the new item comes before the first item in old list
          { item-&gt;next = MyList; MyList = item; return ; }
  
      // Check to see if an item is inserted before the next item
      for ( next = MyList ; next -&gt;next != 0 ; next = next -&gt;next )
          {
          if (strcmp (word, next-&gt;next-&gt;data) &lt; 0 )
              { // Insert Item before the next Item.
              item -&gt;next = next-&gt;next;
              next-&gt;next = item ; return;
              }
          }
  
      // There are no more items ! Add to end
      next -&gt;next = item;
      }
  </code></pre>