Create and insert node in a linear link list using C
24-08-2019    336 times

Create and insert node in a linear link list. When a list has no any node, i.e. the list is empty or list is NULL then need to create a node. Otherwise we have insert node in the existing list. We can insert a node in different position of the list depend on position or value. The type of inserting node are

1. Insert at beginning.
2. Insert at end
3. Insert at a said position except first and last
4. Insert at any position
5. Insert after a value
6. Insert before a value

Now we create the basic environment of the link list.

```#include<stdio.h>
#include<conio.h>
{
int i;
};

//prototype declaration
node *create(node *);
node *insert_first(node *);
void insert_end(node *);
void insert_pos1(node *,int);
node *insert_pos2(node *);
node *insert_after(node *);
node *insert_before(node *);

int main()
{
int c,i,pos;
while(1)
{
printf("\n1. Create a list\n");
printf("2. Insert into begining\n");
printf("3. Insert into end position\n");
printf("4. Insert into position 1\n");
printf("5. Insert into position [Any where]\n");
printf("6. Insert after a said value\n");
printf("7. Insert before a said value\n");
printf("8. Exit\n");
scanf("%d",&c);
switch(c)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
printf("Enter position : ");//Insert at a said position except first and last
scanf("%d",&pos);
break;
case 5:
printf("Enter position : ");//Insert at anywhare
scanf("%d",&pos);
break;
case 6:
break;
case 7:
break;
case 8:
exit(0);
}
}
}
```

Create a node

Function for create the first node of the list. Here we goning to create the first node. i.e. first node or head will be changed after creation of the node. That is why the return type of the function is node.

```node *create(node *l)
{
if(l==NULL)
{
l=(node *)malloc(sizeof(node));//allocate a dynamic memory of node type.
printf("Enter a data : ");
scanf("%d",&l->i);
l->next=NULL;
}
return l;
}
```

Insert a node at begining

Insert a node at the beginning of the exsiting list. If the list is not found the this function call create() function autometically.

```node *insert_first(node *l)
{
node *temp;
if(l!=NULL)
{
temp=(node *)malloc(sizeof(node));
printf("Enter a data : ");
scanf("%d",&temp->i);
temp->next=l;
return temp;
}
else
{
return create(l);
}
}
```

Insert a node at end

Insert a node at the End of the exsiting list. If the list is not found the this function call create() function autometically.

```void insert_end(node *l)
{
node *temp;
if(l!=NULL)
{
temp=(node *)malloc(sizeof(node));
printf("Enter a data : ");
scanf("%d",&temp->i);
while(l->next!=NULL)
{
l=l->next;
}
temp->next=NULL;
l->next=temp;
}
else
{
return create(l);
}
}
```

Insert a node at specific Position But not in first and last

Insert a new node in a said position except first and last. If the list is not found the this function call create() function autometically.

```void insert_pos1(node *l,int pos){
node *temp;
if(l!=NULL)
{
temp=(node *)malloc(sizeof(node));
printf("Enter a data : ");
scanf("%d",&temp->i);
for(i=1;i<=pos-2;i++)
{
l=l->next;
}
temp->next=l->next;
l->next=temp;
}
else
{
return create(l);
}
}
```

Insert a node at any specific Position

Insert a new node in any position of an existing list. If the list is not found the this function call create() function autometically.

```node *insert_pos2(node *l)
{
node *temp,*temp2,*h;
int pos,i,c=0;
h=l;
temp2=l;
printf("Enter position : ");
scanf("%d",&pos);
while(temp2!=NULL)
{
c++;
temp2=temp2->next;
}
if(c>=pos)
{
insert_pos1(l,pos);//normal
return h;
}
else
{
if(l!=NULL)
{
insert_end(l);//last insert
return h;
}
else
{
return create(l);//create
}
}
}
```

Insert a new node after a given value

Insert a new node after a given value. if the value not found in existing list, print an appropiate message. If the list is not found the this function call create() function autometically.

```node *insert_after(node *l)
{
node *temp,*h;
int val;
int fl=0;
h=l;
if(l!=NULL){
printf("Enter a value(after we insert a new node)");
scanf("%d",&val);
while(l->next!=NULL)
{
if(l->i==val)
{
fl=1;
break;
}
l=l->next;
}
if(fl)//found
{
if(l->next==NULL)//last node
{
insert_end(l);//last insert
}
else
{
temp=(node *)malloc(sizeof(node));
temp->next=l->next;
l->next=temp;
}
}
{
}
return h;
}
else
{
return create(l);
}
}
```

Insert a new node before a given value

Insert a new node before a given value. if the value not found in existing list, print an appropiate message. If the list is not found the this function call create() function autometically.

```node *insert_before(node *l)
{
node *temp,*h;
int val;
int fl=0;
h=l;
if(l!=NULL){
printf("Enter a value(before we insert a new node)");
scanf("%d",&val);
while(l->next!=NULL)
{
if(l->next->i==val)
{
fl=1;
break;
}
l=l->next;
}
if(fl)//found
{
if(l==h)//first node
{
return insert_first(l);//insert at begining
}
else
{
temp=(node *)malloc(sizeof(node));
temp->next=l->next;
l->next=temp;
}
}