Skip to content

Commit 1146bf3

Browse files
committed
initial commit
1 parent 8debab5 commit 1146bf3

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

Linked_list_insertionsOperations.c

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct node
5+
{
6+
7+
int data;
8+
struct node *next;
9+
};
10+
11+
void displayList(struct node *ptr)
12+
{
13+
14+
while (ptr != NULL)
15+
{
16+
printf("%d\n", ptr->data);
17+
ptr = ptr->next;
18+
}
19+
}
20+
21+
struct node *insertionatfirst(struct node *head, int data)
22+
{
23+
24+
struct node *ptr = (struct node *)malloc(sizeof(struct node));
25+
ptr->data = data;
26+
27+
ptr->next = head;
28+
head = ptr;
29+
30+
return head;
31+
}
32+
struct node *insertionatlast(struct node *head, int data)
33+
{
34+
35+
struct node *ptr = (struct node *)malloc(sizeof(struct node));
36+
ptr->data = data;
37+
38+
struct node *p = head;
39+
40+
while (p->next != NULL)
41+
{
42+
p = p->next;
43+
}
44+
p->next = ptr;
45+
ptr->next = NULL;
46+
return head;
47+
}
48+
struct node *insertionatindex(struct node *head, int data, int index)
49+
{
50+
51+
struct node *ptr = (struct node *)malloc(sizeof(struct node));
52+
53+
struct node *p = head;
54+
int i = 0;
55+
56+
while (i != index - 1)
57+
{
58+
p = p->next;
59+
i++;
60+
}
61+
62+
ptr->data = data;
63+
ptr->next = p->next;
64+
p->next = ptr;
65+
66+
return head;
67+
}
68+
69+
int main()
70+
{
71+
72+
struct node *head;
73+
struct node *first;
74+
struct node *secound;
75+
struct node *third;
76+
77+
head = (struct node *)malloc(sizeof(struct node));
78+
first = (struct node *)malloc(sizeof(struct node));
79+
secound = (struct node *)malloc(sizeof(struct node));
80+
third = (struct node *)malloc(sizeof(struct node));
81+
82+
head->data = 45;
83+
head->next = first;
84+
85+
first->data = 50;
86+
first->next = secound;
87+
88+
secound->data = 60;
89+
secound->next = third;
90+
91+
third->data = 80;
92+
third->next = NULL;
93+
94+
printf("Before\n");
95+
displayList(head);
96+
printf("AFter\n");
97+
// head = insertionatfirst(head, 70);
98+
// head = insertionatlast(head, 90);
99+
head = insertionatindex(head, 89, 2);
100+
displayList(head);
101+
102+
return 0;
103+
}

0 commit comments

Comments
 (0)