title: How to find the maximum element from the linked list?
body: <p>This is what I have tried so far.</p>
  
  <pre><code>#include&lt;stdlib.h&gt;
  
  struct node
  {
    int data;
    struct node *next;
  };
  
  void create(struct node **q)
  {
    struct node *r,*t;
    int cnt,i,dat;
    printf("How many nodes?");
    scanf("%d",&amp;cnt);
    for(i=0;i&lt;cnt;i++)
      {
         printf("Enter the data %d  ",i);
         scanf("%d",&amp;dat);
         r=(struct node *) malloc (sizeof(struct node));
         r-&gt;data=dat;
         r-&gt;next=NULL;
  
         if(*q==NULL)
         {
           *q=r;
         }
        else
         {
           t=*q;
           while(t-&gt;next!=NULL)
             {
               t=t-&gt;next;
             }
           t-&gt;next=r;
         }
      }
  }
  
  void display(struct node **q)
  {
    struct node *r;
    r=*q;
    while(r!=NULL)
      {
        printf("%d-&gt;",r-&gt;data);
        r=r-&gt;next;
      }
    printf("\n");
  }
  
  void max(struct node **q)
  {
    struct node *r;
    int max=0;
    r=*q;
    while((r-&gt;next)!=NULL)
      {
         max=r-&gt;data;
         r=r-&gt;next;
         if((r-&gt;data)&gt;max)
         {
           max=r-&gt;data;
         }
      }
    printf("The max is %d",max);  
    printf("\n");
  }
  
  int main()
  {
    struct node *head;
    create(&amp;head);
    display(&amp;head);
    max(&amp;head);
  }
  </code></pre>
  
  <p>If I use the numbers 1, 2, and 3 as input my program currently incorrectly detects 2 as the maximum element. How can I fix it so that it will correctly determine 3 to be the maximum element?</p>
  
  <pre><code>rutuparna@pucsd-rutuparna:~/C/Datastructures assignment$ gcc linkedmax.c
  rutuparna@pucsd-rutuparna:~/C/Datastructures assignment$ ./a.out
  How many nodes?3
  Enter the data 0  3
  Enter the data 1  2
  Enter the data 2  1
  3-&gt;2-&gt;1-&gt;
  The max is 2
  </code></pre>