title: How do you display elements of the linked list, without going into a non terminating while loop?
body: <p>I,m writing  a C code for implementing and traversing a Linked list with a while loop.
  I'm unable to figure out what I wrote wrong in code. The code instead of terminating in while (a!=NULL) and displaying all the elements in the linked list,it goes into an infinite loop. here is the code.....</p>
  <pre><code>    #include&lt;stdio.h&gt;
      #include&lt;stdlib.h&gt;
      struct node{
          int data;
          struct node * next;
       };
       
       void Display(struct node * a){
           printf(&quot;The elements are :&quot;);
           while(a!=NULL){
               printf(&quot;%d\n&quot;,a-&gt;data);
               a=a-&gt;next;
           }
      
       }
       int main(){
           int choice;
           struct node * head, * new_node,  * temp;
           head = NULL;  // head points to NULL
                              
           new_node=(struct node*)malloc(sizeof(struct node));
           while(choice){
               
               printf(&quot;Enter the Data&quot;);
               scanf(&quot;%d&quot;, &amp;new_node-&gt;data); // Entering value in new_node
               new_node-&gt;next=NULL;
               if (head == NULL)
               {
      
                   head = temp = new_node;
      
               }
               else
               {
                   temp-&gt;next = new_node;
                   temp = new_node; 
               }
               printf(&quot;Enter 0 for ending and 1 for continuing&quot;);
               scanf(&quot;%d&quot;, &amp;choice);
           }
           Display(head);
           return 0;
       }
  </code></pre>
  <p>Output:
  enter the data1
  enter 0 for ending and 1 for continuing 1
  enter the data 2
  enter 0 for ending and 1 for continuing 1
  enter the data 3
  enter 0 for ending and 1 for continuing 0
  3</p>
  <p>3</p>
  <p>3</p>
  <p>3</p>
  <p>3</p>
  <p>3</p>
  <p>3</p>
  <p>...... not terminating</p>