-
Notifications
You must be signed in to change notification settings - Fork 254
/
25 insert at any pos in dll.c
115 lines (95 loc) · 2.4 KB
/
25 insert at any pos in dll.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
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
#include<stdio.h>
#include<stdlib.h>
struct Node
{
struct Node* prev;
int data;
struct Node* next;
}*first=NULL;
void create(int A[],int n)
{
struct Node* last, * t;
int i;
first = (struct Node*)malloc(sizeof(struct Node));
first->data = A[0]; // Take data from 1st loc of array passed ;
first->prev = first->next = NULL; // set both pointer as NULL;
last=first; // move last to first ;
for (i = 1; i < n; i++)
{
t = (struct Node*)malloc(sizeof(struct Node));
t->data = A[i];
// As i have to Always insert a new node after the last node so ;
//t - next = last->next is same as t->next = head;
t->next = last->next;// this means the newly created node is attached at the end of the list(i.e appended to the last node); // last node will always contain NULL so it is same thing.
t->prev = last;
last->next = t;
last = t;
}
}
void Display(struct Node* p)
{
while (p) //means repeat the loop until p is not equals to null. I.e until we reach the last node, we want to traverse the list
{
printf("->%d ", p->data);
p = p->next;
}
printf("\n");
}
int Length(struct Node* p)
{
int len = 0;
while (p)
{
len++;
p = p->next;
}
return len;
}
// inserting in DLL at a given position
void Insert(struct Node* p, int pos, int x)
{
struct Node* t=NULL;
int i;
if (pos <0 || pos > Length(p))
return;
if (pos == 0) // inserting before 1st node
{
t = (struct Node*)malloc(sizeof(struct Node));
t->data = x;
t->prev = NULL;
t->next = first;
first->prev = t;
first = t;
}
else
{
for (i = 0; i < pos - 1; i++) // move upto pos-1 time to reach particular node
p = p->next;
t = (struct Node*)malloc(sizeof(struct Node)); // creating node
t->data = x; // fill data
t->prev = p;
t->next = p->next;
if (p->next) // Checking wheather there is next node availble or not
p->next->prev = t;
p->next = t;
}
}
int main()
{
int no, i, A[10],pos,x;
printf("enter the size of Array\n ");
scanf_s("%d", &no);
printf("enter the elements of array\n");
for (i = 0; i <no; i++)
scanf_s("%d", &A[i]);
create(A, no);
printf("Given DLL is\n ");
Display(first);
printf("enter pos to insert element\n");
scanf_s("%d", &pos);
scanf_s("%d", &x);
Insert(first, pos, x);
Display(first);
printf("length of linked list is %d", Length(first));
return 0;
}