Insertion in Linked List

http://install4install.com

There are three ways to insert element in the linked list.

The following program deals with insertion in linked list.

Program 1

#include<stdio.h>
#include<stdlib.h>
struct node
{
	int data;
    struct node *next;
}   *head,*var,*trav;

void insert_at_begning(int value)
{
     var=(struct node *)malloc(sizeof (struct node));
     var->data=value;
     if(head==NULL)
     {
         head=var;
         head->next=NULL;
     }
     else
     {
         var->next=head;
         head=var;
     }
}

void insert_at_end(int value)
{
      struct node *temp; 
      temp=head;
      var=(struct node *)malloc(sizeof (struct node));
      var->data=value;
      if(head==NULL)
      {
          head=var;
          head->next=NULL;
      }
      else
      {
          while(temp->next!=NULL)
          {     
               temp=temp->next;
          }
          var->next=NULL;
          temp->next=var;
      }
}
void insert_at_middle(int value, int loc)
{
     struct node *var2,*temp;
     var=(struct node *)malloc(sizeof (struct node));
     var->data=value;
     temp=head;
     if(head==NULL)
     {
          head=var;
          head->next=NULL;
     }
     else
     {
          while(temp->data!=loc)
          {
                temp=temp->next;
          }
          var2=temp->next;
          temp->next=var;
          var->next=var2;
     }
}
void display()
{
     trav=head;
     if(trav==NULL)
     {
          printf("\nList is Empty");
     }
     else
     {
          printf("\nElements in the List: ");
          while(trav!=NULL)
          {
               printf(" -> %d ",trav->data);
               trav=trav->next;
               break;
          }
      printf("\n")
      }

}
int main()
{
     int i=0;
     head=NULL;
     while(1)
     {
           printf("\nenter the choice of operation to perform on linked list");
           scanf("%d",&i);
           switch(i)
           {
                case 1:
                {
                 int value;
                 printf("\nenter the value to be inserted");
                 scanf("%d",&value);
                 insert_at_begning(value);
                 display();                
                 break; 
                }
                case 2:
                {  
                int value;
                printf("\nenter value to be inserted");
                scanf("%d",&value);
                insert_at_end(value);
                display();
                break;
                }
                case 3:
                {
                int value,loc;
                printf("\nafter which data you want to insert the data");
                scanf("%d",&loc);
                printf("\nenter the value to be inserted");
                scanf("%d",&value);
                insert_at_middle(value,loc);
                display();
                break;
                }
                case 6:
                {
                exit(0);
                }
           }
      }
return 0;
}

 

Run

 

Illustration of Program

Explanation of function –> void insert_at_beginning(int value)

Fig 1 : Insert at front

Fig 1 : Insert at front

 

Screen Shot 2014-04-04 at 11.25.01 AM

Fig 4 :Working of void insert_at_beginning

 

 

Explanation of function –>void insert_at_end(int value)

Screen Shot 2014-04-07 at 11.41.22 AM

Fig 3 : Insert at End

 

Screen Shot 2014-04-04 at 11.57.32 AM

Fig 5 : Working of insert_at_end()

 

 

Explanation of  void insert_at_middle(int value, int loc)

Screen Shot 2014-04-04 at 12.18.33 PM

Fig8 : Working of insert_at_middle()

 



Short URL: http://tinyit.cc/fc0e8c
Author: Cusp2207 on April 4, 2014
Category: Computer Science, Data Structures
Tags:
1 response to “Insertion in Linked List”
  1. Cathy says:

    I like your style of writing, the simplicity with which you explain. Keep up the good work. Thanks.

Leave a Reply

Last articles