-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
286 lines (255 loc) · 6.71 KB
/
SinglyLinkedList.java
File metadata and controls
286 lines (255 loc) · 6.71 KB
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
package datastructure.hashtable;
/**
* The type Singly linked list.
*
* @param <T> the type parameter
*/
public class SinglyLinkedList<T> {
/**
* The type Node.
*/
//Node inner class for SLL
public class Node {
/**
* The Data.
*/
public T data;
/**
* The Next node.
*/
public Node nextNode;
}
/**
* The Head node.
*/
//head node of the linked list
public Node headNode;
/**
* The Size.
*/
public int size;
/**
* Instantiates a new Singly linked list.
*/
//constructor
public SinglyLinkedList() {
headNode = null;
size = 0;
}
/**
* Gets head node.
*
* @return the head node
*/
public Node getHeadNode() {
return headNode;
}
/**
* Sets head node.
*
* @param headNode the head node
*/
public void setHeadNode(Node headNode) {
this.headNode = headNode;
}
/**
* Gets size.
*
* @return the size
*/
public int getSize() {
return size;
}
/**
* Sets size.
*
* @param size the size
*/
public void setSize(int size) {
this.size = size;
}
/**
* Is empty boolean.
*
* @return the boolean
*/
public boolean isEmpty() {
if (headNode == null) return true;
return false;
}
/**
* Insert at head.
*
* @param data the data
*/
//Inserts new data at the start of the linked list
public void insertAtHead(T data) {
//Creating a new node and assigning it the new data value
Node newNode = new Node();
newNode.data = data;
//Linking head to the newNode's nextNode
newNode.nextNode = headNode;
headNode = newNode;
size++;
}
/**
* Insert at end.
*
* @param data the data
*/
//Inserts new data at the end of the linked list
public void insertAtEnd(T data) {
//if the list is empty then call insertATHead()
if (isEmpty()) {
insertAtHead(data);
return;
}
//Creating a new Node with value data
Node newNode = new Node();
newNode.data = data;
newNode.nextNode = null;
Node last = headNode;
//iterate to the last element
while (last.nextNode != null) {
last = last.nextNode;
}
//make newNode the next element of the last node
last.nextNode = newNode;
size++;
}
/**
* Insert after.
*
* @param data the data
* @param previous the previous
*/
//inserts data after the given prev data node
public void insertAfter(T data, T previous) {
//Creating a new Node with value data
Node newNode = new Node();
newNode.data = data;
//Start from head node
Node currentNode = this.headNode;
//traverse the list until node having data equal to previous is found
while (currentNode != null && currentNode.data != previous) {
currentNode = currentNode.nextNode;
}
//if such a node was found
//then point our newNode to currentNode's nextElement
if (currentNode != null) {
newNode.nextNode = currentNode.nextNode;
currentNode.nextNode = newNode;
size++;
}
}
/**
* Print list.
*/
public void printList() {
if (isEmpty()) {
System.out.println("List is Empty!");
return;
}
Node temp = headNode;
System.out.print("List : ");
while (temp.nextNode != null) {
System.out.print(temp.data.toString() + " -> ");
temp = temp.nextNode;
}
System.out.println(temp.data.toString() + " -> null");
}
/**
* Search node boolean.
*
* @param data the data
* @return the boolean
*/
//Searches a value in the given list.
public boolean searchNode(T data) {
//Start from first element
Node currentNode = this.headNode;
//Traverse the list till you reach end
while (currentNode != null) {
if (currentNode.data.equals(data))
return true; //value found
currentNode = currentNode.nextNode;
}
return false; //value not found
}
/**
* Delete at head.
*/
//Deletes data from the head of list
public void deleteAtHead() {
//if list is empty then simply return
if (isEmpty())
return;
//make the nextNode of the headNode equal to new headNode
headNode = headNode.nextNode;
size--;
}
/**
* Delete by value.
*
* @param data the data
*/
//Deletes data given from the linked list
public void deleteByValue(T data) {
//if empty then simply return
if (isEmpty())
return;
//Start from head node
Node currentNode = this.headNode;
Node prevNode = null; //previous node starts from null
if(currentNode.data.equals(data)) {
//data is at head so delete from head
deleteAtHead();
return;
}
//traverse the list searching for the data to delete
while (currentNode != null) {
//node to delete is found
if (data.equals(currentNode.data)){
prevNode.nextNode = currentNode.nextNode;
}
prevNode = currentNode;
currentNode = currentNode.nextNode;
}
}
/**
* Reverse.
*/
//Reverses the linked list
public void reverse() {
Node prev = null; //To keep track of the previous element, will be used in swapping links
Node current = this.headNode; //firstElement
Node next = null;
//While Traversing the list, swap links
while (current != null) {
next = current.nextNode;
current.nextNode = prev;
prev = current;
current = next;
}
//Linking Head Node with the new First Element
this.headNode = prev;
}
/**
* Remove duplicates.
*/
public void removeDuplicates() {
Node current = headNode; // will be used for outer loop
Node compare = null; // will be used for inner loop
while (current != null && current.nextNode != null) {
compare = current;
while (compare.nextNode != null) {
if (current.data == compare.nextNode.data) {
compare.nextNode = compare.nextNode.nextNode;
} else {
compare = compare.nextNode;
}
}
current = current.nextNode;
}
}
}