-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.java
197 lines (181 loc) · 5.17 KB
/
DoublyLinkedList.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
package linear;
public class DoublyLinkedList{
DNode head;
DNode tail; // Additional reference to tail, this improved the insert operation from O(n) to O(1).
// Inserts new node at the end of the Linked List
//O(1)
DNode insert(int value){
return this.insertLast(value);
}
// Inserts new node with given key, value at the end of the Linked List
//O(1)
DNode insert(int key, int value){
return this.insertLast(key, value);
}
//O(1)
DNode insertLast(int value){
DNode newItem = new DNode(value);
if(head == null){
head = newItem;
tail = newItem;
} else{
newItem.prev = tail;
tail.next = newItem;
tail = newItem;
}
return newItem;
}
//O(1)
DNode insertLast(int key, int value){
DNode newItem = new DNode(key, value);
if(head == null){
head = newItem;
tail = newItem;
} else{
newItem.prev = tail;
tail.next = newItem;
tail = newItem;
}
return newItem;
}
//O(1)
DNode insertFirst(int value){
DNode newItem = new DNode(value);
if(head == null){
head = newItem;
tail = newItem;
} else {
newItem.next = head;
head.prev = newItem; // Link back from the old head to the new head
head = newItem;
}
return newItem;
}
//O(1)
DNode insertFirst(int key, int value){
DNode newItem = new DNode(key, value);
if(head == null){
head = newItem;
tail = newItem;
} else {
newItem.next = head;
head.prev = newItem; // Link back from the old head to the new head
head = newItem;
}
return newItem;
}
//O(1)
boolean deleteFirst(){
if(head == null) return false;
if(head == tail){
head = null;
tail = null;
}else{
head = head.next;
head.prev = null;
}
return true;
}
//O(1) uding tail reference
boolean deleteLast(){
if(head == null) return false;
if(head == tail){
head = null;
tail = null;
}else{
tail = tail.prev;
tail.next = null;
}
return true;
}
// Iterates over the Doubly Linked List sequentially and returns the node if the item data equals node data. Returns null if not found.
DNode find(int data){
if(head == null) return null;
DNode current = head;
while(current != null){
if(current.data == data) return current;
current = current.next;
}
return null;
}
// Returns true if the delete of the given data node is successfull else false.
boolean delete(int data){
if(head == null) return false;
DNode current = head;
while(current != null){
if(current.data == data){
if(current.prev !=null){
current.prev.next = current.next;
if(current.next != null) {
current.next.prev = current.prev;
} else {
tail = current.prev; // Update tail if last node is deleted
}
}else{
head = head.next;// Move head to the next node
if(head != null){
head.prev = null; // Update prev of the new head
} else {
tail = null; // If list becomes empty
}
}
return true;// Deletion successful
}
current = current.next;
}
return false; // Data not found in the list
}
void print(){
DNode current = head;
while(current !=null){
System.out.print(current.data +" ");
current = current.next;
}
System.out.println();
System.out.println("Current Head "+head.data);
System.out.println("Current Tail "+tail.data);
}
void reverse(){
DNode prev = null;
DNode current = head;
DNode oldHead = head;
while(current != null){
DNode temp = current.next;
current.next = prev;
current.prev = temp;
prev = current;
current = temp;
}
head = prev;
tail = oldHead;
}
public static void main(String args[]){
DoublyLinkedList dll = new DoublyLinkedList();
dll.insertLast(1);
dll.insertLast(2);
dll.insertLast(3);
dll.insertLast(4);
dll.insertLast(5);
dll.print();
dll.reverse();
dll.print();
}
}
class DNode{
int key; //used when key value pair is required like in a LRU implementaion
int data;
DNode next;
DNode prev;
DNode(int data) {
this.key = data;
this.data = data;
this.next = null;
this.prev = null;
}
DNode(int key, int data) {
this.key = key;
this.data = data;
this.next = null;
this.prev = null;
}
}