-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleLinkedList.java
297 lines (265 loc) · 8.32 KB
/
DoubleLinkedList.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// The Double linked list node
class Node{
int data;
Node next;
Node prev;
Node(int x){
this.data=x;
this.next=null;
this.prev=null;
}
}
public class DoubleLinkedList {
// the head and tail of the linked list
Node head;
Node tail;
// function to add a new node to the last of linked list
// Time Complexity - O(1), Space Complexity - O(1)
public void addNode(int x){
Node newNode = new Node(x);
// if linked list is empty
if(head==null){
head=newNode;
tail=newNode;
return;
}
// else if linked list is not empty
tail.next=newNode;
newNode.prev=tail;
tail=newNode;
}
// function to traverse the linked list from left to right
// Time Complexity - O(n), Space Complexity - O(1)
public void traverseForward(){
// if linked list is empty
if(head==null){
System.out.println("Linked list is empty");
return;
}
// else if linked list is not empty
Node temp=head;
while(temp!=null){
System.out.print(temp.data+" ");
temp=temp.next;
}
System.out.println();
}
// function to traverse the linked list from right to left
// Time Complexity - O(n), Space Complexity - O(1)
public void traverseBackward(){
// if linked list is empty
if(head==null){
System.out.println("Linked list is empty");
return;
}
// if linked list is not empty
Node temp=tail;
while(temp!=null){
System.out.print(temp.data+" ");
temp=temp.prev;
}
System.out.println();
}
// function to get total no of nodes present in linked list
// Time Complexity - O(n), Space Complexity - O(1)
private int getCountOfNodes(){
int count=0;
Node temp=head;
while(temp!=null){
count++;
temp=temp.next;
}
return count;
}
// function to insert a new node with value as 'data' in linked list at specified location 'loc'
// Time Complexity - O(n), Space Complexity - O(1)
public void insertNodeByLoc(int data, int loc){
int countOfNodes = getCountOfNodes();
// check validity of loc
if(loc<=0 || loc>countOfNodes+1){
System.out.println("Not a valid location to insert");
return;
}
// if loc is valid:
Node newNode = new Node(data);
// if insert at start
if(loc==1){
newNode.next=head;
head.prev=newNode;
head=newNode;
return;
}
// if insert at end
else if(loc==countOfNodes+1){
tail.next=newNode;
newNode.prev=tail;
tail=newNode;
return;
}
// if insert anywhere in between
else{
Node temp=head;
// bring temp to one node before loc node
for(int i=0;i<loc-2;i++){
temp=temp.next;
}
// do the linking and unlinking
newNode.next=temp.next;
temp.next.prev=newNode;
temp.next=newNode;
newNode.prev=temp;
}
}
// function to delete a node at specified location
// Time Complexity - O(n), Space Complexity - O(1)
public void deleteNodeByLoc(int loc){
// if linked list is empty
if(head==null){
System.out.println("Linked List is empty");
return;
}
// else if linked list is not empty
int countOfNodes = getCountOfNodes();
// check if given loc is valid is not
if(loc<=0 || loc>countOfNodes+1){
System.out.println("Not a valid location to delete");
return;
}
// if node to be deleted is head node
if(loc==1){
head=head.next;
head.prev=null;
return;
}
// if node to be deleted is tail node
else if(loc==countOfNodes){
tail=tail.prev;
tail.next=null;
}
// if node to be deleted is anywhere in between
else{
// bring the temp node to one node before the node to be deleted
Node temp=head;
for(int i=0;i<loc-2;i++){
temp=temp.next;
}
// here currentNode is the node to be deleted
Node currentNode = temp.next;
// do the linking and unlinking
temp.next=currentNode.next;
currentNode.next.prev=temp;
return;
}
}
// function to search for a node with given data 'x' and return that node if present
// Time Complexity - O(n), Space Complexity - O(1)
public Node searchNode(int x){
// if linked list is empty
if(head==null){
System.out.println("Linked List is empty");
return null;
}
// else if linked list is not empty
Node temp=head;
// traverse through the linked list and see if the node with given data is there or not
while(temp!=null){
// if found then return that node
if(temp.data==x){
return temp;
}
temp=temp.next;
}
// if not found return null
return null;
}
// function to delete a node only on the basis of the value given
// Time Complexity - O(n), Space Complexity - O(1)
public void deleteNodeByValue(int x){
// if linked list is empty
if(head==null){
System.out.println("Linked list is empty");
return;
}
// else if linked list is not empty
// search for the node with given value
Node nodeToDelete = searchNode(x);
// if node with given value is not found in linked list
if(nodeToDelete==null){
System.out.println("Node with given data is not present in linked list");
return;
}
// if node with given value is found in linked list:
// if node to be deleted is head node
if(nodeToDelete==head){
head=head.next;
head.prev=null;
return;
}
// if node to be deleted is tail node
else if(nodeToDelete==tail){
tail = tail.prev;
tail.next=null;
return;
}
// if node to be deleted is anywhere in between head and tail node
else{
Node previousNode=nodeToDelete.prev;
previousNode.next=nodeToDelete.next;
nodeToDelete.next.prev=previousNode;
return;
}
}
// function to delete complete linked list
// Time Complexity - O(n), Space Complexity - O(1)
public void deleteCompleteLinkedList(){
// if linked list is empty
if(head==null){
System.out.println("Linked List is already empty, hence can't delete Linked List");
return;
}
// first we remove all the prev links
Node temp=head;
while(temp!=null){
temp.prev=null;
temp=temp.next;
}
// Now we make the head null adn tail null
head=null;
tail=null;
}
public static void main(String[] args) throws Exception {
DoubleLinkedList ll = new DoubleLinkedList();
ll.addNode(1);
ll.addNode(2);
ll.addNode(3);
ll.addNode(4);
ll.traverseForward();
ll.traverseBackward();
ll.insertNodeByLoc(0,5);
ll.traverseForward();
ll.traverseBackward();
ll.deleteNodeByLoc(3);
ll.traverseForward();
ll.traverseBackward();
System.out.println(ll.searchNode(1));
ll.deleteNodeByValue(0);
ll.traverseForward();
ll.traverseBackward();
ll.deleteCompleteLinkedList();
ll.traverseForward();
ll.traverseBackward();
}
}
/* ============================== OUTPUT ===================================
1 2 3 4
4 3 2 1
1 2 3 4 0
0 4 3 2 1
1 2 4 0
0 4 2 1
Node@6b884d57
1 2 4
4 2 1
Linked list is empty
Linked list is empty
==============================================================================*/