title: finding smallest two nodes in a linked list in c
body: <p>I am trying to find the smallest two numbers in a linked list in C++ (without using any built-in functions).</p>
  
  <p>I tried to do so as below:</p>
  
  <p><strong>My logic is:</strong></p>
  
  <p>(1) Assume the first node in the linked list is <code>minimum1</code> and the second is <code>minimum2</code>. Compare them. The greater becomes <code>minimum2</code> and the smaller becomes <code>minimum1</code>.</p>
  
  <p>(2) Start from the third node (third because we have already covered first and second) in a while loop until we reach <code>NULL</code>, thus traversing all the list.</p>
  
  <p>(3) Compare the newly traversed node with <code>minimum1</code> and <code>minimum2</code>. If this node is smaller than <code>minimum1</code>, then put its value in <code>minimum1</code>. <code>minimum2</code> now will contain the value of <code>minimum1</code>, and <code>minimum1</code> will contain the value of the newly found node which was smaller than <code>minumum1</code>.</p>
  
  <p>Below is my code which takes the number of nodes to be created, reads the values of all nodes continuously (just keep on pressing <strong>Enter</strong> after every number), and creates the linked list from all the nodes. These are working fine.</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;malloc.h&gt;
  #include &lt;string.h&gt;
  
  struct node
  {
      int freq;
      struct node* next;
  };
  typedef struct node node;
  node* tree;
  
  // Finding minimum two elements:
  void find_two_min(node** List, node** lmin1, node** lmin2)
  {
      int i;
      node* temp = *List;
      node *min1, *min2;
      node* var1 = *List;
      node* second = (*List)-&gt;next;
      if (var1 &gt; second)
      {
          min2 = var1;
          min1 = second;
      }
      else
      {
          min1 = var1;
          min2 = second;
      }
      while (temp-&gt;next-&gt;next-&gt;next != NULL)
      {
          if (temp-&gt;freq &lt; min2-&gt;freq)
          {
              min1 = min2;
              min2 = temp;
          }
          else if (temp-&gt;freq &lt; min1-&gt;freq)
          {
              min1 = temp;
          }
          temp = temp-&gt;next;
      }
      *lmin1 = min1;
      *lmin2 = min2;
  }
  
  void main()
  {
      int size, data;
      node *min1, *min2;
      int count = 0; // This flag is to check whether it's the first node inside the do-while loop.
      tree = NULL;
      printf("Enter the number of nodes:\n");
      scanf("%d", &amp;size);
      printf("Enter the elements:\n");
      node* prev;
      do
      {
          scanf("%d", &amp;data);
          if (count == 0)
          {
              node* temp;
              temp = (node*) malloc(sizeof(node));
              temp-&gt;freq = data;
              temp-&gt;next = NULL;
              prev = temp;
              tree = prev;
          }
          else
          {
              node* temp;
              temp = (node*) malloc(sizeof(node));
              temp-&gt;freq = data;
              temp-&gt;next = NULL;
              prev-&gt;next = temp;
              prev = prev-&gt;next;
          }
          --size;
          ++count;
      }
      while (size &gt; 0);
  
      printf("Printing linked list:\n");
      node* temp1;
      temp1 = tree;
      while (temp1 != NULL)
      {
          printf("%d, ", temp1-&gt;freq);
          temp1 = temp1-&gt;next;
      }
      node* temp5 = tree;
      find_two_min(&amp;temp5, &amp;min1, &amp;min2);
      printf("\nThe two minimum numbers are min1: %d and min2: %d.\n", min1-&gt;freq, min2-&gt;freq);
  }
  </code></pre>
  
  <p>My code doesn't work on the following input (it gives the wrong output):</p>
  
  <pre><code>Enter the number of nodes:
  4
  Enter the elements:
  0
  -5
  -2
  8
  Printing linked list:
  0, -5, -2, 8,
  The two minimum numbers are min1: 0 and min2: -5.
  </code></pre>
  
  <p>It was supposed to print "min1: -5" and "min2: -2" but I don't know why it doesn't.</p>
  
  <p>Could anyone please help me eliminate this problem? Any algorithm or piece of code in C/C++ to use as a reference is appreciated. Thanks. </p>
  
  <p>Note: I can't use any built-in functions.</p>