title: Calculate the sum, avg and nodes of a linked list in c
body: <p>I have this linked list c program right here. It lets the user enter as many numbers as possible until 0 is entered. The list is then displayed and sorted numerically from smallest to biggest....</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct node{
      int data;
      struct node *ptr;
  } node;
  
  node * insert(node *head, int num)
  {
      node *temp = malloc(sizeof(node));
      temp-&gt;data = num;
  
      if (head == NULL || head-&gt;data &gt; num)
      {
          temp-&gt;ptr = head;
          head = temp;
      }
      else
      {
          node *current = head;
          while ((current-&gt;ptr != NULL) &amp;&amp; !(num &lt; current-&gt;ptr-&gt;data))
  
          {
              current = current-&gt;ptr;
          }
  
          temp-&gt;ptr = current-&gt;ptr;
          current-&gt;ptr = temp;
      }
  
      return head;
  }
  
  node * delete_if_less( node *head, int data )
  {
      while ( head != NULL &amp;&amp; head-&gt;data &lt; data )
      {
          node *temp = head;
          head = head-&gt;ptr;
          free( temp );
      }
  
      if ( head != NULL )
      {
          for ( node *current = head; current-&gt;ptr != NULL; )
          {
              if ( current-&gt;ptr-&gt;data &lt; data )
              {
                  node *temp = current-&gt;ptr;
                  current-&gt;ptr = current-&gt;ptr-&gt;ptr;
                  free( temp );
              }
              else
              {
                  current = current-&gt;ptr;
              }
          }
      }
  
      return head;
  }
  
  void free_list(node *head) {
      node *prev = head;
      node *cur = head;
      while(cur) {
          prev = cur;
          cur = prev-&gt;ptr;
          free(prev);
      }
  }
  
  int main(){
      int num, min;
      node *head, *p;
      head = NULL;
  
      do {
          printf(&quot;: &quot;);
          scanf(&quot;%d&quot;,&amp;num);
          if(num) {
              head = insert(head, num);
              for ( p = head; p != NULL; p = p-&gt;ptr )
              {
                  printf(&quot;%d &quot;, p-&gt;data);
              }
              printf(&quot;\n&quot;);
          }
      } while(num);
  
      p = head;
      printf(&quot;\n:\n&quot;);
  
      while(p) {
          printf(&quot;%d &quot;, p-&gt;data);
          p = p-&gt;ptr;
      }
      free_list(head);
  
      return 0;
  }
  </code></pre>
  <p>My next step would be 1) calculating the sum of the numbers in the list 2) calculating how many numbers are in the list and finally 3) the arithmetic average of the numbers in the list. This is how it should look like...</p>
  <pre><code>: 1
  1 
  : 2
  1 2
  : 3
  1 2 3 
  : 1
  1 1 2 3
  : 5
  1 1 2 3 5
  : 0
  1 1 2 3 5
  
  total sum: 12
  elements: 5
  average: 2.4
  </code></pre>
  <p>My question is how do I make and calculate the sum, average and numbers of nodes of a linked list like this??</p>