title: How to perform insert, delete, and display an element into a linked list in C?
body: <p>I'm working on a program in C which is not yet so familiar to me. It has choices on what to do with the linked list. But it has errors. So far, this is what I have. </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  struct node
  {
      int data;
      struct node *next;
  }*head;
  
  
  
  void append(int num)
  {
      struct node *temp,*right;
      temp= (struct node *)malloc(sizeof(struct node));
      temp-&gt;data=num;
      right=(struct node *)head;
      while(right-&gt;next != NULL)
      right=right-&gt;next;
      right-&gt;next =temp;
      right=temp;
      right-&gt;next=NULL;
  }
  
  
  
  void add( int num )
  {
      struct node *temp;
      temp=(struct node *)malloc(sizeof(struct node));
      temp-&gt;data=num;
      if (head== NULL)
      {
      head=temp;
      head-&gt;next=NULL;
      }
      else
      {
      temp-&gt;next=head;
      head=temp;
      }
  }
  void addafter(int num, int loc)
  {
      int i;
      struct node *temp,*left,*right;
      right=head;
      for(i=1;i&lt;loc;i++)
      {
      left=right;
      right=right-&gt;next;
      }
      temp=(struct node *)malloc(sizeof(struct node));
      temp-&gt;data=num;
      left-&gt;next=temp;
      left=temp;
      left-&gt;next=right;
      return;
  }
  
  int count()
  {
      struct node *n;
      int c=0;
      n=head;
      while(n!=NULL)
      {
      n=n-&gt;next;
      c++;
      }
      return c;
  } 
  
  
  void insert(int num)
  {
      int c=0;
      struct node *temp;
      temp=head;
      if(temp==NULL)
      {
      add(num);
      }
      else
      {
      while(temp!=NULL)
      {
          if(temp-&gt;data&lt;num)
          c++;
          temp=temp-&gt;next;
      }
      if(c==0)
          add(num);
      else if(c&lt;count())
          addafter(num,++c);
      else
          append(num);
      }
  }
  
  
  
  int delete(int num)
  {
      struct node *temp, *prev;
      temp=head;
      while(temp!=NULL)
      {
      if(temp-&gt;data==num)
      {
          if(temp==head)
          {
          head=temp-&gt;next;
          free(temp);
          return 1;
          }
          else
          {
          prev-&gt;next=temp-&gt;next;
          free(temp);
          return 1;
          }
      }
      else
      {
          prev=temp;
          temp= temp-&gt;next;
      }
      }
      return 0;
  }
  
  
  void  display(struct node *r)
  {
      r=head;
      if(r==NULL)
      {
      return;
      }
      while(r!=NULL)
      {
      printf("%d ",r-&gt;data);
      r=r-&gt;next;
      }
      printf("\n");
  }
  
  
  int  main()
  {
      int i,num;
      struct node *n;
      head=NULL;
      while(1)
      {
      printf("\nList Operations\n");
      printf("===============\n");
      printf("1.Insert\n");
      printf("2.Display\n");
      printf("3.Size\n");
      printf("4.Delete\n");
      printf("5.Exit\n");
      printf("Enter your choice : ");
      if(scanf("%d",&amp;i)&lt;=0){
          printf("Enter only an Integer\n");
          exit(0);
      } else {
          switch(i)
          {
          case 1:      printf("Enter the number to insert : ");
                   scanf("%d",&amp;num);
                   insert(num);
                   break;
          case 2:     if(head==NULL)
                  {
                  printf("List is Empty\n");
                  }
                  else
                  {
                  printf("Element(s) in the list are : ");
                  }
                  display(n);
                  break;
          case 3:     printf("Size of the list is %d\n",count());
                  break;
          case 4:     if(head==NULL)
                  printf("List is Empty\n");
                  else{
                  printf("Enter the number to delete : ");
                  scanf("%d",&amp;num);
                  if(delete(num))
                      printf("%d deleted successfully\n",num);
                  else
                      printf("%d not found in the list\n",num);
                  }
                  break;
          case 5:     return 0;
          default:    printf("Invalid option\n");
          }
      }
      }
      return 0;
  }
  </code></pre>