-
Notifications
You must be signed in to change notification settings - Fork 1
/
Making_lisnked_list_using_insert_function.c
68 lines (68 loc) · 1.46 KB
/
Making_lisnked_list_using_insert_function.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
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *first = NULL;
void create(int A[], int n)
{
first = (struct node *)malloc(sizeof(struct node));
(*(first)).data = A[0];
(*(first)).next = NULL;
struct node *t, *last;
last = first;
for (int i = 1; i < n; i++)
{
t = (struct node *)malloc(sizeof(struct node));
(*(t)).data = A[i];
(*(t)).next = NULL;
(*(last)).next = NULL;
last = t;
}
}
void display(struct node *p)
{
while (p != NULL)
{
printf("%d\t", (*(p)).data);
p = (*(p)).next;
}
}
void insert(struct node *p, int pos, int num)
{
struct node *t;
t = (struct node *)malloc(sizeof(struct node));
(*(t)).data = num;
if (pos == 0)
{
(*(t)).next = first;
first = t;
}
else
{
struct node *q = NULL;
q = first;
for (int i = 0; i < pos - 1; i++)
{
q = (*(q)).next;
}
(*(t)).next = (*(q)).next;
(*(q)).next = t;
}
}
int main()
{
printf("How many numbers do you want to insert?\n");
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("\nEnter position and value:\n");
int pos, val;
scanf("%d %d", &pos, &val);
insert(first, pos, val);
printf("\n NEW LINKED LIST:\n");
display(first);
}
}