-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doubly_Linked_LIST.java
228 lines (214 loc) · 5.85 KB
/
Doubly_Linked_LIST.java
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
222
223
224
225
226
227
228
class DLL {
class node {
int data;
node next;
node prev;
node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
node first = null;
void insertfirst(int data) {
node n = new node(data);
if (first == null) {
first = n;
} else {
n.next = first;
first.prev = n;
first = n;
}
}
void insertlast(int data) {
node n = new node(data);
if (first == null) {
first = n;
} else {
node c = first;
while (c.next != null) {
c = c.next;
}
c.next = n;
n.prev = c;
}
}
void insertbefore(int data, int value) {
if (first == null) {
System.out.println("List is Empty..");
} else {
int flag = 0;
node c = first;
while (c.next != null) {
if (c.data == value) {
flag = 1;
}
c = c.next;
}
if (flag == 0) {
System.out.println("Value not found..");
} else {
if (first.data == value) {
insertfirst(data);
} else {
node c1 = first;
while (c1.data != value) {
c1 = c1.next;
}
node n = new node(data);
n.next = c1;
c1.prev.next = n;
n.prev = c1.prev;
c1.prev = n;
}
}
}
}
void insertafter(int data, int value) {
if (first == null) {
System.out.println("List is Empty..");
} else {
int flag = 0;
node c = first;
while (c != null) {
if (c.data == value) {
flag = 1;
}
c = c.next;
}
if (flag == 0) {
System.out.println("Value does not exist..");
} else {
node c1 = first;
while (c1.data != value) {
c1 = c1.next;
}
if (c1.next == null) {
insertlast(data);
} else {
node n = new node(data);
n.next = c1.next;
c1.next.prev = n;
c1.next = n;
n.prev = c1;
}
}
}
}
void deletefirst() {
if (first == null) {
System.out.println("List is Empty..");
} else {
if (first.next == null) {
first = null;
} else {
node del = first;
first.next.prev = null;
first = first.next;
del.next = null;
}
}
}
void deletelast() {
if (first == null) {
System.out.println("List is Empty");
} else {
if (first.next == null) {
first = null;
} else {
node c = first;
while (c.next != null) {
c = c.next;
}
node del = c;
c.prev.next = null;
del.prev = null;
}
}
}
void deletevalue(int value) {
if (first == null) {
System.out.println("List is Empty");
} else {
int flag = 0;
node c = first;
while (c != null) {
if (c.data == value) {
flag = 1;
}
c = c.next;
}
if (flag == 0) {
System.out.println("Value not found..");
} else {
if (first.data == value) {
deletefirst();
} else {
node c1 = first;
while (c1.data != value) {
c1 = c1.next;
}
if (c1.next == null) {
deletelast();
} else {
node del = c1;
c1.prev.next = c1.next;
c1.next.prev = c1.prev;
del.prev = null;
del.next = null;
}
}
}
}
}
void display() {
if (first == null) {
System.out.println("List is Empty");
} else {
node c = first;
System.out.print("null<-->");
while (c != null) {
System.out.print(c.data + "<-->");
c = c.next;
}
System.out.print("null");
System.out.println();
}
}
void displayreverse() {
if (first == null) {
System.out.println("List is Empty");
} else {
node c = first;
System.out.print("null<-->");
while (c.next != null) {
c = c.next;
}
while (c != null) {
System.out.print(c.data + "<-->");
c = c.prev;
}
System.out.print("null");
System.out.println();
}
}
}
class Main2 {
public static void main(String[] args) {
DLL d = new DLL();
d.insertfirst(30);
d.insertfirst(20);
d.insertfirst(10);
d.insertlast(40);
d.insertbefore(25, 30);
d.insertafter(35, 30);
d.insertafter(45, 40);
// d.deletefirst();
// d.deletelast();
// d.deletevalue(30);
// d.deletevalue(20);
// d.deletevalue(40);
d.display();
d.displayreverse();
}
}