-
Notifications
You must be signed in to change notification settings - Fork 0
/
09.Insertion_between_linked_list.c
62 lines (49 loc) · 1.33 KB
/
09.Insertion_between_linked_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node * next;
};
void linkListTravers(struct Node *head){
int count = 0;
while(head != NULL){
printf("Element %d : %d\n", count, head->data);
head = head->next;
count++;
}
printf("\n");
}
struct Node * insertionBetween(struct Node *head, int data, int index){
struct Node * new_node = (struct Node *)malloc(sizeof(struct Node));
struct Node * ptr = head;
int i = 0;
while(i < (index-1)){
ptr = ptr->next;
i++;
}
new_node->data = data;
new_node->next = ptr->next;
ptr->next = new_node;
return head;
}
int main(){
struct Node *head, *second, *third, *fourth;
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
fourth = (struct Node *)malloc(sizeof(struct Node));
head->data = 7;
head->next = second;
second->data = 23;
second->next = third;
third->data = 12;
third->next = fourth;
fourth->data = 57;
fourth->next = NULL;
linkListTravers(head);
int new_data = 11, index = 2;
printf("Insert %d in index %d\n", new_data, index);
head = insertionBetween(head, new_data, index);
linkListTravers(head);
return 0;
}