title: Adding elements linked list
body: <p>I have the following *addEngine function that adds elements to my linked list, which I want to sort in ascending order by year. I must not change the function arguments.</p>
  
  <p>It doesn't work the way I imagine. Why is</p>
  
  <pre><code> a = AddEngine ( a, createEngine ( "TDI 1.9", 2015) );
  </code></pre>
  
  <p>not included when printf in main?</p>
  
  <pre><code>      typedef struct TEngine 
      {
          struct TEngine *m_Next;
          int m_Year;
          char m_Type [ TYPE_MAX ];
      } TENGINE;
  
        typedef struct TArchive
      {
          struct TArchive *m_Next;
          TENGINE *m_Engines;
      } TARCHIVE;
  
        TARCHIVE *AddEngine  ( TARCHIVE *list, TENGINE *engine )
      {
  
        TARCHIVE * res = ( TARCHIVE * ) malloc ( sizeof ( *res ) );
  
        if ( list == NULL )
     {
           res -&gt; m_Next = NULL;
           res -&gt; m_Engines = engine;
           return res;
     }
  
       int currentYear = list-&gt;m_Engines-&gt;m_Year;
  
      // The inserted element is smaller than the first
       if ( currentYear &gt; engine-&gt;m_Year )
    {      
           res -&gt; m_Next = list;
           res -&gt; m_Engines = engine;
           return res;
    }
  
      TARCHIVE *prev = list;
      TARCHIVE *current = list -&gt; m_Next;
  
      while ( current != NULL )
    {
          currentYear = current-&gt;m_Engines-&gt;m_Year;
  
              if ( currentYear &gt;= engine-&gt;m_Year )  
         {
                 res -&gt; m_Next = prev -&gt; m_Next;
                 res -&gt; m_Engines = engine;
                 prev -&gt; m_Next = res;
  
                 return list;
         }
  
        prev = current;
        current = current -&gt; m_Next;
    }
  
    prev -&gt; m_Next = res;
    res -&gt; m_Engines = engine;
    res -&gt; m_Next = NULL;
    return res;
   }
  
    return 0;
  }
     int main ( void )
   {
        TARCHIVE *a, *b;
        b = a = NULL;
        a = AddEngine ( a, createEngine ( "TDI 1.9", 2015) );
        a = AddEngine ( a, createEngine ( "TDI 1.8", 3001 ) );
        a = AddEngine ( a, createEngine ( "TSI 1.2", 1999 ) );
        a = AddEngine ( a, createEngine ( "TDI 2.0", 2108 ) );
        a = AddEngine ( a, createEngine ( "TDI 1.9", 2005) );
        b = a = AddEngine ( a, createEngine ( "TDI 1.8", 1010 ) );
        a = AddEngine ( a, createEngine ( "TDI 1.9", 2025) );
        a = AddEngine ( a, createEngine ( "TDI 1.8", 2038 ) );
  
        while ( b )
      {
          printf ("%s", b -&gt; m_Engines -&gt; m_Type);
          b = b -&gt; m_Next;``
      }
  </code></pre>