-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDouble_LL.c
186 lines (175 loc) · 3.88 KB
/
Double_LL.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
#include<stdio.h>
#include<stdlib.h>
struct DNode
{
int data;
struct DNode *prev, *next;
};
struct DNode *start = NULL;
struct DNode *getnode()
{
return ((struct DNode *)malloc(sizeof(struct DNode)));
}
void createList()
{
start = getnode();
start->prev = NULL;
start->next = NULL;
printf("\nEnter Data: ");
scanf("%d", &start->data);
struct DNode *q, *p;
q = getnode();
p = start;
while(1)
{
int ch;
printf("\nPress 1 - Continue and 0 - Exit: ");
scanf("%d", &ch);
if (ch == 1)
{
printf("\nEnter Data: ");
scanf("%d", &q->data);
q->next = NULL;
q->prev = p;
p->next = q;
p = q;
}
else
break;
}
}
void insertBeg()
{
struct DNode *q;
q = getnode();
printf("\nEnter element to insert at the beginning: ");
scanf("%d", &q->data);
q->next = start;
q->prev = NULL;
if (start != NULL)
{
start->prev = q;
}
start = q;
}
void insertEnd()
{
struct DNode *q, *p;
q = getnode();
printf("\nEnter element to insert at the end: ");
scanf("%d", &q->data);
q->next = NULL;
if (start == NULL)
{
q->prev = NULL;
start = q;
}
else
{
p = start;
while (p->next != NULL)
{
p = p->next;
}
q->prev = p;
p->next = q;
}
}
void insertNPos()
{
struct DNode *p, *q;
int pos;
q = getnode();
if (start == NULL)
{
printf("\nList is Empty\n");
return;
}
printf("\nEnter the position where you want to insert: ");
scanf("%d", &pos);
if (pos < 1)
{
printf("\nInvalid Position\n");
return;
}
if (pos == 1)
{
insertBeg();
return;
}
p = start;
for (int i = 1; i < pos-1 && p != NULL; i++)
{
p = p->next;
}
if (p == NULL)
{
printf("\nPosition does not Exists\n");
return;
}
printf("Enter the element you want to insert: ");
scanf("%d", &q->data);
q->next = p->next;
if (p->next != NULL)
{
p->next->prev = q;
}
q->prev = p;
p->next = q;
}
void display()
{
struct DNode *p;
if (start == NULL)
{
printf("\nList is Empty\n");
return;
}
p = start;
printf("\nList Elements: ");
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
int choice;
do
{
printf("\n1 - Create Double Linked List");
printf("\n2 - Insert element from the begining");
printf("\n3 - Insert element from the end");
printf("\n4 - Insert element from the Nth Position");
printf("\n5 - Delete element from the begining");
printf("\n6 - Delete element from the end");
printf("\n7 - Delete element from the Nth Position");
printf("\n8 - Display Double Linked List");
printf("\n9 - EXIT");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: createList();
break;
case 2: insertBeg();
break;
case 3: insertEnd();
break;
case 4: insertNPos();
break;
// case 5: deleteBeg();
// break;
// case 6: deleteEnd();
// break;
// case 7: deletePos();
// break;
case 8: display();
break;
case 9: exit(1);
default: printf("\nINVALID CHOICE\n");
}
}while (choice != 0);
}