-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathDoubly Linked List.c
221 lines (190 loc) · 4.9 KB
/
Doubly 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *prev;
int data;
struct node *next;
};
// function declaration for displaying the doubly linked list
void display_doubly_list(struct node *q)
{
if (q == NULL) // checking whether the list is empty or not
{
printf("List is empty!");
}
else
{
while (q != NULL)
{
printf("%d\n", q->data);
q = q->next;
}
}
}
// function declaration of insert at end
void insert_at_beg(struct node **q, int num)
{
struct node *p, *r;
r = *q; // storing the head in r
p = (struct node *)malloc(sizeof(struct node)); // creating new node
p->prev = NULL; // initially the value of *q is NULL after then it will store the value of the next node
p->data = num; // inputting the data
p->next = r; // storing the head value in p->next
*q = p; // storing the address of new node in head
display_doubly_list(*q); // displaying the data in doubly linked list
}
// function declaration of inserting data at the end of the list
void insert_at_end(struct node **q, int num)
{
struct node *p, *r;
r = *q;
if (r == NULL)
{
// insert_at_beg function
struct node *p, *r;
r = *q;
p = (struct node *)malloc(sizeof(struct node));
p->prev = NULL;
p->data = num;
p->next = r;
*q = p;
}
else
{
while (r->next != NULL)
{
r = r->next;
}
p = (struct node *)malloc(sizeof(struct node));
p->prev = r;
p->data = num;
p->next = NULL;
r->next = p;
}
display_doubly_list(*q); // displaying the data in doubly linked list
}
// function declaration of inserting data at a specific position of the list
void insert_at(struct node **q, int position, int num)
{
struct node *p, *r;
int count = 1;
r = *q;
if (r == NULL)
{
// insert_at_beg function
struct node *p, *r;
r = *q;
p = (struct node *)malloc(sizeof(struct node));
p->prev = NULL;
p->data = num;
p->next = r;
*q = p;
}
else
{
while (count != position - 1)
{
r = r->next;
count++;
}
p = (struct node *)malloc(sizeof(struct node));
p->prev = r;
p->data = num;
p->next = r->next;
r->next = p;
}
display_doubly_list(*q); // displaying the data in doubly linked list
}
// function declaration for deletion of data at the beginning of the list
void delete_at_beg(struct node **q)
{
struct node *r;
r = *q;
if (r == NULL)
{
printf("No data to delete in the list.\n");
}
else
{
*q = r->next;
free(r);
}
display_doubly_list(*q); // displaying the data in doubly linked list
}
//function declaration of deleting data at the end of the list
void delete_at_end(struct node **q)
{
struct node *r, *s;
r = *q;
if (r == NULL)
{
printf("No data to delete in the list.\n");
}
else
{
while (r->next != NULL)
{
s = r;
r = r->next;
}
s->next = NULL;
free(r);
}
display_doubly_list(*q); // displaying the data in doubly linked list
}
// main function
int main()
{
struct node *head;
head = NULL;
int choice, data, position;
printf("Enter the operation to perform : \n");
printf(" 1. Insert at beginning \n 2. Insert at end \n 3. Insert at \n 4. Delete at beginning \n 5. Delete at end \n 6. Delete at \n 7. Display \n 8. Exit");
while (1)
{
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the data to insert at beginning : ");
scanf("%d", &data);
insert_at_beg(&head, data);
break;
case 2:
printf("Enter the data to insert at end : ");
scanf("%d", &data);
insert_at_end(&head, data);
break;
case 3:
printf("Enter the position to insert : ");
scanf("%d", &position);
printf("Enter the data to insert : ");
scanf("%d", &data);
insert_at(&head, position, data);
break;
case 4:
delete_at_beg(&head);
break;
case 5:
delete_at_end(&head);
break;
case 6:
printf("Enter the position to delete : ");
scanf("%d", &position);
delete_at(&head, position);
break;
case 7:
display_doubly_list(head);
break;
case 8:
exit(0);
break;
default:
printf("Please enter a valid choice!\n");
break;
}
}
return 0;
}