title: C++ linked list implementation multiple data insertion
body: <p>I'm trying to create a subject pre-requisite checker using linked list. I know how to insert single data into a node.</p>
  
  <p>My problem is how to insert multiple data into a node? I found a good example which fits my assignment perfectly. But the problem is I do not understand C very much. Can any one help to explain the <code>void add()</code> function below? I want to use that <code>add</code> function to my assignment. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;malloc.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt;
  
  struct node
  {
     char data [ 20 ];
     char m [ 5 ] [ 20 ];
     int mcount;
     struct node * link;
  };
  
  struct node * dic [ 26 ];
  
  void add ( char * );
  int  search ( char * );
  void show( );
  void deldic( );
  
  void main( )
  {
     char word [ 20 ] , ch;
     int i;
  
     clrscr( );
  
     while ( 1 )
     {
         clrscr( );
         printf ( "\n\t\tDictionary\n" );
         printf ( "\n\t\t1.Add Word.\n" );
         printf ( "\t\t2.Search Word.\n" );
         printf ( "\t\t3.Show Dictionary.\n" );
         printf ( "\t\t0.Exit." );
         printf ( "\n\n\t\tYour Choice ");
         scanf ( "%d", &amp;ch );
  
         switch ( ch )
         {
             case 1 :
  
                 printf ( "\nEnter any word : " );
                 fflush ( stdin );
                 gets ( word );
                 add ( word );
  
                 break;
  
             case 2 :
  
                 printf ( "\nEnter the word to search : " );
                 fflush ( stdin );
                 gets ( word );
                 i = search ( word );
                 if ( ! i )
                     printf ( "Word does not exists." );
                 getch( );
  
                 break;
  
             case 3 :
  
                 show( );
                 getch( );
  
                 break;
  
             case 0 :
  
                 deldic( );
                 exit ( 0 );
  
             default :
  
                 printf ( "\nWrong Choice" );
         }
     }
  }
  
  void add ( char * str )
  {
     int i, j = toupper ( str [ 0 ] ) - 65;
     struct node * r, * temp = dic [ j ], * q;
     char mean [ 5 ] [ 20 ], ch = 'y';
  
     i = search ( str );
     if ( i )
     {
         printf ( "\nWord already exists." );
         getch( );
         return;
     }
     q = ( struct node * ) malloc ( sizeof ( struct node ) );
     strcpy ( q -&gt; data, str );
     q -&gt; link = NULL;
  
     for ( i = 0; tolower ( ch ) == 'y' &amp;&amp; i &lt; 5; i++ )
     {
         fflush ( stdin );
         printf ( "\n\nEnter the meaning(s) : " );
         gets ( mean [ i ] );
         strcpy ( q -&gt; m [ i ] , mean [ i ] );
         if ( i != 4 )
             printf ( "\nAdd more meanings (y/n) " );
         else
             printf ( "You cannot enter more than 5 meanings." );
         fflush ( stdin );
         ch = getche( );
     }
  
     q -&gt; mcount = i;
     if ( dic [ j ] == NULL || strcmp ( dic [ j ] -&gt; data, str ) &gt; 0 )
     {
         r = dic [ j ];
         dic [ j ] = q;
         q -&gt; link = r;
         return;
     }
  
     else
     {
         while ( temp != NULL )
         {
             if ( ( strcmp ( temp -&gt; data, str ) &lt; 0 ) &amp;&amp; ( ( strcmp ( temp -&gt; link -&gt; data, str ) &gt; 0 ) ||
                                             temp -&gt; link == NULL ) )
             {
                 q -&gt; link = temp -&gt; link;
                 temp -&gt; link = q;
                 return;
             }
             temp = temp -&gt; link;
         }
     }
  }
  </code></pre>
  
  <p>Here is my assignment so far </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  
  struct subjectlist
  {
     string subject;
     string prereq;
     subjectlist *next;
  };
  
  subjectlist *start_prt=NULL;
  subjectlist *current;
  int option=0;
  
  int main ()
  {
   int x;
   string subject;
  
                 cout &lt;&lt; "1. Add subject" &lt;&lt; endl;
                 cout &lt;&lt; "2. Search prerequisite" &lt;&lt; endl;
                 cout &lt;&lt; "3. Delete subject" &lt;&lt; endl;
                 cout &lt;&lt; "4.Show subjects" &lt;&lt; endl;
                 cout &lt;&lt; "5. Save to file" &lt;&lt; endl;
                 cout &lt;&lt; "6. Load from file" &lt;&lt; endl;
                   cout &lt;&lt; "0. Exit" &lt;&lt; endl;
                 cin &gt;&gt; x;
  
                 switch (x)
     {
         case 1:
             cout&lt;&lt;"Input subject"&lt;&lt;endl;
             cin &gt;&gt;  subject;
             add(subject);
             break;
  
         case 2:
             cout&lt;&lt;"Input subject to be checked"&lt;&lt;endl;
             break;
  
         case 3:
             cout&lt;&lt;"Delete a subject"&lt;&lt;endl;
             break;
  
         case 4:
             cout&lt;&lt;"Show Subjects"&lt;&lt;endl;
             break;
  
         case 5:
             cout&lt;&lt;"Save to File"&lt;&lt;endl;
             break;
  
         case 6:
             cout&lt;&lt;"Load from file"&lt;&lt;endl;
             break;
  
         case 0:
             cout&lt;&lt;"exit"&lt;&lt;endl;
             break;
  
  
         default: cout &lt;&lt;"Invalid selection, please try again."&lt;&lt;endl;
     }
   }
  
  void add ()
  {
  
  }
  </code></pre>