Saturday, 18 March 2017

C Program to Insert a Node in Singly Linked List

C Program to insert Node in Singly Linked List is given below. In this program we take two input from the user the first input is the element to be inserted in the Linked List and second input is the position at which node is to be inserted.

Approach:
Let insert(int item,int pos)
1.  if(pos==1):
2.            create a temporary node
3.            set   temp.data=item
4.            set   temp.next=head
5.            set   head=temp
6. Else:
7.            set   current=head
8.            set   count=0
9.            do While current!=NULL:
10.                     set  count=count+1
11.                     If   count==pos-1:
12.                                        create a temporary node
13.                                        set   temp.data=item
14.                                        set   temp.next=current.next
15.                                        set   current.next=temp
16.                                         break
17.                     Else:
18.                                       set   count=count+1
19.                                       set   current=current.next
20.                    Ebd IfElse
21.          EndWhile
22. End IfElse

Program


#include <stdio.h>
#include <stdlib.h>
void insert(int element,int pos);
void print();
struct node{
   int data;
   struct node *next;
}*head=NULL,*temp;
int main()
{
  int element,pos;
  temp=(struct node*)malloc(sizeof(struct node));
     temp->data=12;
     temp->next=NULL;
     head=temp;
     temp->next=(struct node*)malloc(sizeof(struct node));
     temp=temp->next;
     temp->data=56;
     temp->next=(struct node*)malloc(sizeof(struct node));
     temp=temp->next;
     temp->data=33;
     temp->next=(struct node*)malloc(sizeof(struct node));
     temp=temp->next;
     temp->data=44;
     temp->next=(struct node*)malloc(sizeof(struct node));
     temp=temp->next;
     temp->data=526;
     temp->next=(struct node*)malloc(sizeof(struct node));
     temp=temp->next;
     temp->data=6;
     temp->next=NULL;

     /*calling print function to print the original Linked List*/
     printf("\nLinked List before insertion: \n");
     print();
     printf("\n\nEnter the element to be insert in the Linked List: ");
     scanf("%d",&element);
     printf("\nEnter the position at which element to be inserted: ");
     scanf("%d",&pos);
     insert(element,pos);
     printf("\n\nLinked List after insertion:\n");
     print();
     printf("\n");
     return 0;
}
void insert(int element,int pos)
{
     int count=0;
     struct node *current;
  if(pos==1)
  {
       temp=(struct node*)malloc(sizeof(struct node));
       temp->data=element;
       temp->next=head;
       head=temp;
  }
  else
  {
        current=head;
        while(current!=NULL)
        {
              count++;
              if(count==pos-1)
              {
                       temp=(struct node*)malloc(sizeof(struct node));
                       temp->data=element;
                       temp->next=current->next;
                       current->next=temp;
                       break;
              }
              else
              {
                    
                    current=current->next;
              }
        }
  }
}
void print()
{
   struct node *current;
   current=head;
   while(current!=NULL)
   {
         printf("%d->",current->data);
         current=current->next;
   }
}


OUTPUT:

 


EmoticonEmoticon