If you want to make money online : Register now

# Write an algorithm for linked list to :insert an element after a given element.

, ,

1.insert an element after a given
element.

In order to insert an element after the specified number of nodes into the linked list, we need to skip the desired number of elements in the list to move the pointer at the position after which the node will be inserted. This will be done by using the following statements.

2.             for(i=0;i<loc;i++)
3.             {
4.                 temp = temp->next;
5.                 if(temp == NULL)
6.                 {
7.                     return;
8.                 }
9.
10.             }
• Allocate the space for the new node and add the item to the data part of it. This will be done by using the following statements.
1. ptr = (struct node *) malloc (sizeof(struct node));
2.         ptr->data = item;
• Now, we just need to make a few more link adjustments and our node at will be inserted at the specified position. Since, at the end of the loop, the loop pointer temp would be pointing to the node after which the new node will be inserted. Therefore, the next part of the new node ptr must contain the address of the next part of the temp (since, ptr will be in between temp and the next of the temp). This will be done by using the following statements.
1. ptr→ next = temp → next

now, we just need to make the next part of the temp, point to the new node ptr. This will insert the new node ptr, at the specified position.

1. temp ->next = ptr;

## Algorithm

• STEP 1: IF PTR = NULL
• STEP 2: SET NEW_NODE = PTR
• STEP 3: NEW_NODE → DATA = VAL
• STEP 4: SET TEMP = HEAD
• STEP 5: SET I = 0
• STEP 6: REPEAT STEP 5 AND 6 UNTIL I
• STEP 7: TEMP = TEMP → NEXT
• STEP 8: IF TEMP = NULL
• STEP 9: PTR → NEXT = TEMP → NEXT
• STEP 10: TEMP → NEXT = PTR
• STEP 11: SET PTR = NEW_NODE
• STEP 12: EXIT

## C Function

1. #include<stdio.h>
2. #include<stdlib.h>
3. void randominsert(int);
4. void create(int);
5. struct node
6. {
7.     int data;
8.     struct node *next;
9. };
11. void main ()
12. {
13.     int choice,item,loc;
14.     do
15.     {
16.         printf("\nEnter the item which you want to insert?\n");
17.         scanf("%d",&item);
19.         {
20.             create(item);
21.         }
22.         else
23.         {
24.             randominsert(item);
25.         }
26.         printf("\nPress 0 to insert more ?\n");
27.         scanf("%d",&choice);
28.     }while(choice == 0);
29. }
30. void create(int item)
31. {
32.
33.         struct node *ptr = (struct node *)malloc(sizeof(struct node *));
34.         if(ptr == NULL)
35.         {
36.             printf("\nOVERFLOW\n");
37.         }
38.         else
39.         {
40.             ptr->data = item;
43.             printf("\nNode inserted\n");
44.         }
45. }
46. void randominsert(int item)
47.     {
48.         struct node *ptr = (struct node *) malloc (sizeof(struct node));
49.         struct node *temp;
50.         int i,loc;
51.         if(ptr == NULL)
52.         {
53.             printf("\nOVERFLOW");
54.         }
55.         else
56.         {
57.
58.             printf("Enter the location");
59.             scanf("%d",&loc);
60.             ptr->data = item;
62.             for(i=0;i<loc;i++)
63.             {
64.                 temp = temp->next;
65.                 if(temp == NULL)
66.                 {
67.                     printf("\ncan't insert\n");
68.                     return;
69.                 }
70.
71.             }
72.             ptr ->next = temp ->next;
73.             temp ->next = ptr;
74.             printf("\nNode inserted");
75.         }
76.
77.     }

Output

```Enter the item which you want to insert?
12

Node inserted

Press 0 to insert more ?
2```