Skip to content

Commit ad74cfe

Browse files
author
Deepak Malik
committed
Linked List
1 parent 0212a78 commit ad74cfe

File tree

3 files changed

+72
-38
lines changed

3 files changed

+72
-38
lines changed

src/com/deepak/data/structures/LinkedList/CircularLinkedList.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Data-Structures-in-Java
2+
* Data-Structures-In-Java
33
* CircularLinkedList.java
44
*/
55
package com.deepak.data.structures.LinkedList;
@@ -61,7 +61,8 @@ public void insertAtBeginning(E value) {
6161
*/
6262
public void insertAtTail(E value) {
6363
Node<E> newNode = new Node<E>(value);
64-
if (null == head) { /* When list is empty */
64+
if (null == head) {
65+
/* When list is empty */
6566
head = newNode;
6667
} else {
6768
Node<E> temp = head;
@@ -83,7 +84,8 @@ public void insertAtTail(E value) {
8384
public void insertAtPosition(E value, int position) {
8485
if (position < 0 || position > size) {
8586
throw new IllegalArgumentException("Position is Invalid");
86-
} /* Conditions check passed, let's insert the node */
87+
}
88+
/* Conditions check passed, let's insert the node */
8789
Node<E> newNode = new Node<E>(value);
8890
Node<E> tempNode = head;
8991
for (int i = 0; i < position; i++) {

src/com/deepak/data/structures/LinkedList/DoublyLinkedList.java

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* Data-Structures-in-Java
2+
* Data-Structures-In-Java
3+
* DoublyLinkedList.java
34
*/
45
package com.deepak.data.structures.LinkedList;
56

@@ -48,7 +49,8 @@ public class DoublyLinkedList<E> {
4849
*/
4950
public void insertAtHead(E value) {
5051
Node<E> newNode = new Node<E>(value);
51-
if (null == head) { /* If list is empty */
52+
if (null == head) {
53+
/* If list is empty */
5254
newNode.next = null;
5355
newNode.prev = null;
5456
head = newNode;
@@ -77,7 +79,8 @@ public void insertAtHead(E value) {
7779
*/
7880
public void insertAtTail(E value) {
7981
Node<E> newNode = new Node<E>(value);
80-
if (null == tail) { /* If list is empty */
82+
if (null == tail) {
83+
/* If list is empty */
8184
newNode.next = null;
8285
newNode.prev = null;
8386
head = newNode;
@@ -110,18 +113,23 @@ public void insertAtTail(E value) {
110113
public void insertAtPosition(E value, int position) {
111114
if (position < 0 || position > size) {
112115
throw new IllegalArgumentException("Position is Invalid");
113-
} /* Conditions check passed, let's insert the node */
114-
if (position == 0) { /* Insertion should happen at head */
116+
}
117+
/* Conditions check passed, let's insert the node */
118+
if (position == 0) {
119+
/* Insertion should happen at head */
115120
insertAtHead(value);
116-
} else if (position == size -1) { /* Insertion should happen at tail */
121+
} else if (position == size -1) {
122+
/* Insertion should happen at tail */
117123
insertAtTail(value);
118-
} else { /* Insertion is happening somewhere in middle */
124+
} else {
125+
/* Insertion is happening somewhere in middle */
119126
Node<E> currentNode = head;
120127
for (int i = 0; i < position; i++) {
121128
currentNode = currentNode.next;
122129
}
123130
Node<E> previousNode = currentNode.prev;
124-
/* Insertion of new node will happen in between previous node and current node */
131+
/* Insertion of new node will happen in
132+
* between previous node and current node */
125133
Node<E> newNode = new Node<E>(value);
126134
newNode.next = currentNode;
127135
newNode.prev = previousNode;
@@ -156,7 +164,7 @@ public void traverseBackward() {
156164
/**
157165
* Returns the size of the linked list
158166
*
159-
* @return
167+
* @return {@link int}
160168
*/
161169
public int size() {
162170
return size;
@@ -165,7 +173,7 @@ public int size() {
165173
/**
166174
* Returns true, if linked list is empty
167175
*
168-
* @return
176+
* @return {@link boolean}
169177
*/
170178
public boolean isEmpty() {
171179
return size == 0;
@@ -177,15 +185,17 @@ public boolean isEmpty() {
177185
* exception is thrown.
178186
*
179187
* @param index
180-
* @return
188+
* @return {@link Node<E>}
181189
*/
182190
public Node<E> searchByIndex(int index) {
183191
if (index < 0 || index >= size) {
184192
throw new IndexOutOfBoundsException("Invalid index passed while searching for a value");
185-
} /* Validation passed, let's search for value using the index */
193+
}
194+
/* Validation passed, let's search for value using the index */
186195
Node<E> temp = head;
187-
for (int i = 0; i < index; i++) { /* Start from 0 and go till one less then index
188-
because we are jumping to next node inside the loop */
196+
for (int i = 0; i < index; i++) {
197+
/* Start from 0 and go till one less then index
198+
* because we are jumping to next node inside the loop */
189199
temp = temp.next;
190200
}
191201
return temp;
@@ -197,9 +207,10 @@ public Node<E> searchByIndex(int index) {
197207
* in linked list, first one will be returned.
198208
*
199209
* @param value
200-
* @return
210+
* @return {@link Node<E>}
201211
*/
202-
public Node<E> searchByValue(E value) { /* Traverse through each node until this value is found */
212+
public Node<E> searchByValue(E value) {
213+
/* Traverse through each node until this value is found */
203214
Node<E> temp = head;
204215
while (null != temp.next && temp.item != value) {
205216
temp = temp.next;
@@ -214,7 +225,8 @@ public Node<E> searchByValue(E value) { /* Traverse through each node until this
214225
* Delete's the element present at head node
215226
*/
216227
public void deleteFromHead() {
217-
if (null == head) { /* If list is empty, return */
228+
/* If list is empty, return */
229+
if (null == head) {
218230
return;
219231
}
220232
Node<E> temp = head;
@@ -227,7 +239,8 @@ public void deleteFromHead() {
227239
* Delete's the element present at tail node
228240
*/
229241
public void deleteFromTail() {
230-
if (null == tail) { /* If list is empty, return */
242+
/* If list is empty, return */
243+
if (null == tail) {
231244
return;
232245
}
233246
Node<E> temp = tail;
@@ -244,7 +257,8 @@ public void deleteFromTail() {
244257
public void deleteFromPosition(int position) {
245258
if (position < 0 || position >= size) {
246259
throw new IllegalArgumentException("Position is Invalid");
247-
} /* Conditions check passed, let's delete the node */
260+
}
261+
/* Conditions check passed, let's delete the node */
248262
Node<E> nodeToBeDeleted = head;
249263
for (int i = 0; i < position; i++) {
250264
nodeToBeDeleted = nodeToBeDeleted.next;

src/com/deepak/data/structures/LinkedList/SinglyLinkedList.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Data-Structures-in-Java
2+
* Data-Structures-In-Java
33
* SinglyLinkedList.java
44
*/
55
package com.deepak.data.structures.LinkedList;
@@ -65,8 +65,10 @@ public void insertAtHead(E value) {
6565
*/
6666
public void insertAtTail(E value) {
6767
Node<E> newNode = new Node<E>(value);
68-
newNode.next = null; /* Since this insertion is at tail, this new node will point to null */
69-
if (null == head) { /* When list is empty */
68+
newNode.next = null;
69+
/* Since this insertion is at tail, this new node will point to null */
70+
if (null == head) {
71+
/* When list is empty */
7072
head = newNode;
7173
} else {
7274
Node<E> tempNode = head;
@@ -95,7 +97,8 @@ public void insertAtTail(E value) {
9597
public void insertAtPosition(E value, int position) {
9698
if (position < 0 || position > size) {
9799
throw new IllegalArgumentException("Position is Invalid");
98-
} /* Conditions check passed, let's insert the node */
100+
}
101+
/* Conditions check passed, let's insert the node */
99102
Node<E> newNode = new Node<E>(value);
100103
if (position == 0) {
101104
newNode.next = head;
@@ -125,7 +128,7 @@ public void traverse() {
125128
/**
126129
* Returns size of the linked list
127130
*
128-
* @return
131+
* @return {@link int}
129132
*/
130133
public int size() {
131134
return size;
@@ -134,7 +137,7 @@ public int size() {
134137
/**
135138
* Returns true is list is empty
136139
*
137-
* @return
140+
* @return {@link boolean}
138141
*/
139142
public boolean isEmpty() {
140143
return size == 0;
@@ -146,15 +149,17 @@ public boolean isEmpty() {
146149
* exception is thrown.
147150
*
148151
* @param index
149-
* @return
152+
* @return {@link Node<E>}
150153
*/
151154
public Node<E> searchByIndex(int index) {
152155
if (index < 0 || index >= size) {
153156
throw new IndexOutOfBoundsException("Invalid index passed while searching for a value");
154-
} /* Validation passed, let's search for value using the index */
157+
}
158+
/* Validation passed, let's search for value using the index */
155159
Node<E> temp = head;
156-
for (int i = 0; i < index; i++) { /* Start from 0 and go till one less then index
157-
because we are jumping to next node inside the loop */
160+
for (int i = 0; i < index; i++) {
161+
/* Start from 0 and go till one less then index
162+
* because we are jumping to next node inside the loop */
158163
temp = temp.next;
159164
}
160165
return temp;
@@ -166,9 +171,10 @@ public Node<E> searchByIndex(int index) {
166171
* in linked list, first one will be returned.
167172
*
168173
* @param value
169-
* @return
174+
* @return {@link Node<E>}
170175
*/
171-
public Node<E> searchByValue(E value) { /* Traverse through each node until this value is found */
176+
public Node<E> searchByValue(E value) {
177+
/* Traverse through each node until this value is found */
172178
Node<E> temp = head;
173179
while (null != temp.next && temp.item != value) {
174180
temp = temp.next;
@@ -183,9 +189,11 @@ public Node<E> searchByValue(E value) { /* Traverse through each node until this
183189
* Delete's the element present at head node
184190
*/
185191
public void deleteFromHead() {
186-
if (null == head) { /* If list is empty, return */
192+
/* If list is empty, return */
193+
if (null == head) {
187194
return;
188195
}
196+
/* Update head and reduce size */
189197
head = head.next;
190198
size--;
191199
}
@@ -194,16 +202,21 @@ public void deleteFromHead() {
194202
* Delete's the element present at tail node
195203
*/
196204
public void deleteFromTail() {
205+
/* If head is null, nothing to delete */
197206
if (null == head) {
198207
return;
199208
}
209+
/* Keep a pointer on head node and next node.
210+
* Keep going until next becomes null */
200211
Node<E> currentNode = head;
201212
Node<E> nextNode = currentNode.next;
202213
while (currentNode.next != null && nextNode.next != null) {
203214
currentNode = currentNode.next;
204215
nextNode = nextNode.next;
205216
}
217+
/* Now we are removing from tail, so tail - 1 node will point to null */
206218
currentNode.next = null;
219+
/* Reduce the size */
207220
size--;
208221
}
209222

@@ -215,12 +228,14 @@ public void deleteFromTail() {
215228
public void deleteFromPosition(int position) {
216229
if (position < 0 || position >= size) {
217230
throw new IllegalArgumentException("Position is Invalid");
218-
} /* Conditions check passed, let's delete the node */
231+
}
232+
/* Conditions check passed, let's delete the node */
219233
Node<E> nodeToBeDeleted = head;
220234
for (int i = 0; i < position; i++) {
221235
nodeToBeDeleted = nodeToBeDeleted.next;
222236
}
223-
if (nodeToBeDeleted.next == null) { /* If this is a last node */
237+
if (nodeToBeDeleted.next == null) {
238+
/* If this is a last node */
224239
deleteFromTail();
225240
} else {
226241
nodeToBeDeleted.item = nodeToBeDeleted.next.item;
@@ -232,12 +247,15 @@ public void deleteFromPosition(int position) {
232247
* Returns a array containing each element
233248
* from the list from start to end
234249
*
235-
* @return
250+
* @return {@link Object[]}
236251
*/
237252
public Object[] toArray() {
253+
/* Create an array of object of same size */
238254
Object[] result = new Object[size];
239255
int i = 0;
240256
for (Node<E> x = head; x != null; x = x.next) {
257+
/* Take each node and add it to the array
258+
* at appropriate position*/
241259
result[i++] = x.item;
242260
}
243261
return result;

0 commit comments

Comments
 (0)