-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListInsertion.c
More file actions
120 lines (111 loc) · 2.84 KB
/
LinkedListInsertion.c
File metadata and controls
120 lines (111 loc) · 2.84 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void LinkdeListTraversal(struct Node *ptr)
{
// printf("Data in linked list is: \n");
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
// Case 1:
struct Node *insertAtBeginning(struct Node *head, int data)
{
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->data = data;
ptr->next = head;
return ptr;
}
// Case 2:
struct Node *insertAtIndex(struct Node *head, int index, int data)
{
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
p = head;
int i = 0;
while (i != index - 1)
{
p = p->next;
i++;
}
ptr->data = data;
ptr->next = p->next;
p->next = ptr;
return head;
}
// Case 3:
struct Node *insertAtEnd(struct Node *head, int data)
{
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
p = head;
ptr->data = data;
while (p->next != NULL)
{
/* code */
p = p->next;
}
p->next = ptr;
ptr->next = NULL;
return head;
}
// Case 4:
struct Node *insertAfter(struct Node *head, struct Node *prevNode, int data)
{
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->data = data;
ptr->next = prevNode->next;
prevNode->next = ptr;
return head;
}
int main()
{
struct Node *head;
struct Node *second;
struct Node *third;
struct Node *fourth;
struct Node *fifth;
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));
fifth = (struct Node *)malloc(sizeof(struct Node));
head->data = 20;
head->next = second;
second->data = 30;
second->next = third;
third->data = 40;
third->next = fourth;
fourth->data = 50;
fourth->next = fifth;
fifth->data = 60;
fifth->next = NULL;
printf("Original List: ");
LinkdeListTraversal(head);
printf("\n");
printf("After Insertion after a Node: ");
head = insertAfter(head, second, 70);
LinkdeListTraversal(head);
printf("\n");
head = insertAtBeginning(head, 10);
printf("After Insertion at beginning: ");
LinkdeListTraversal(head);
printf("\n");
printf("After Insertion at given index: ");
head = insertAtIndex(head, 2, 25);
LinkdeListTraversal(head);
printf("\n");
printf("After Insertion at given index: ");
head = insertAtIndex(head, 5, 45);
LinkdeListTraversal(head);
printf("\n");
printf("After Insertion at end: ");
head = insertAtEnd(head, 70);
LinkdeListTraversal(head);
}