title: linked list using double pointers
body: <p>Using double pointers first time to create and display linked list</p>
  
  <pre><code>#include "stdio.h"
  #include "stdlib.h"
  
  struct node
  {
      int data;
      struct node * next;
  };
  void Insert(struct node **, int , int );
  void display(struct node *);
  int main()
  {
      int c, data, position;
      struct node* head;
      do{
          printf("Enter a choice :\n");
          printf("1. Add an element.\n");
          printf("2. Del an element.\n3.Display List.\n");
          printf("4.Delete linked list.\n5.Exit.\n");
          printf("Your Choice :");
          scanf("%d",&amp;c);
          switch(c){
              case 1 :
                  printf("\nEnter data and position :\n");
                  scanf("%d %d",&amp;data,&amp;position);
                  Insert(&amp;head,data,position);
                  break;
              case 2 :
  
                  break;
              case 3 :
                  printf("Linked List : \n");
                  display(head);
                  break;
              case 4 :
  
                  break;
              case 5 :
                  exit(0);
              default :
                  printf("Invalid Choice.\n");
                  break;
          }
      }while(1);
  
      return 0;
  }
  
  
  void Insert(struct node **ptrhead, int item, int position){
      struct node *p,*newnode;
      //node creation.
      newnode = (struct node *)malloc(sizeof(struct node));
      if (!newnode)
      {
          printf("Memory Error.\n");
          return;
      }
      newnode-&gt;next = NULL;
      newnode-&gt;data = item;
  
      p = *ptrhead;
  
      // Creates initial node
      if (!(p-&gt;data))
          {
              p = newnode;
          }
  
      // insertion at beginning
      if (position==1)
      {
              newnode-&gt;next = p;
              p = newnode;
              free(newnode);
      }
      // insertionn at middle or end.
      else
      {
          int i=1;
          while(p-&gt;next!=NULL &amp;&amp; i&lt;position-1){
              p=p-&gt;next;
              i++;
          }
          newnode-&gt;next = p-&gt;next;
          p-&gt;next = newnode;
      }
  
      *ptrhead = p;
  
  };
  
  // Display Linked list
  void display(struct node *head){
      if (head)
      {
          do{
              printf("%d\n", head-&gt;data);
              head = head-&gt;next;
          }while(head-&gt;next);
      }
  
  };
  </code></pre>
  
  <p>I will add functions for deletion and other operations later. Right now , I just want to insert and display fns to work . But output comes as infinitely running loop with wrong values. I cannot figure out what's wrong in my code , please help ?
  Thanks in advance.</p>