-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleLinkedList.java
More file actions
334 lines (306 loc) · 8.53 KB
/
SingleLinkedList.java
File metadata and controls
334 lines (306 loc) · 8.53 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
public class SingleLinkedList<E> {
private Node<E> head;
private int size;
/** private inner class */
private static class Node<E>
{
private E data;
private Node<E> next;
/** Creates a new node with a null next field
@param dataItem The data stored
*/
private Node(E dataItem)
{
data = dataItem;
next = null;
}
/** Creates a new node that references another node
@param dataItem The data stored
@param nodeRef The node referenced by new node
*/
private Node(E dataItem, Node<E> nodeRef)
{
data = dataItem;
next = nodeRef;
}
} //end class Node
public SingleLinkedList( )
{
// initially empty list
head = null;
size = 0;
}
public int size()
{
return size;
}
private void addFirst(E item)
{
Node<E> temp = new Node<E>(item, head);
head = temp;
size++;
}
private void addAfter(Node<E> node, E item)
{
Node<E> temp = new Node<E>(item, node.next);
node.next = temp;
size++;
}
private E removeAfter(Node<E> node)
{
Node<E> temp = node.next;
if (temp != null)
{
node.next = temp.next;
size--;
return temp.data;
}
else
return null;
}
private E removeFirst()
{
Node<E> node = head;
if (head != null)
{
head = head.next;
size--;
return node.data;
}
else
return null;
}
private Node<E> getNode(int index)
{
Node<E> node = head;
for (int i = 0; i < index && node != null; i++) {
node = node.next;
}
return node;
}
public void printContent(int index)
{
Node<E> node = getNode(index);
if (node != null)
System.out.println(node.data);
}
// Return the object at the specified index
public E get(int index)
{
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
Node<E> node = getNode(index);
return node.data;
}
// Replace the element at the specified index by the
// new value anEntry and return the old value
public E set (int index, E anEntry)
{
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
Node<E> node = getNode(index); // calling private method
E result = node.data;
node.data = anEntry;
return result;
}
// Method to insert a new item at the specified index in
// the list
public void add (int index, E item)
{
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
if (index == 0)
addFirst(item);
else {
Node<E> node = getNode(index - 1);
addAfter(node, item);
}
}
// Method to insert a new item (node) at the end of the list
public boolean add (E item)
{
add(size, item); // calling public method add
return true;
}
// Remove the node at the given index and return its data
public E remove (int index)
{
if (index < 0 || index >= size) {
throw new
IndexOutOfBoundsException(Integer.toString(index));
}
E item;
if (index == 0)
item = removeFirst( );
else {
Node<E> node = getNode(index - 1);
item = removeAfter(node);
}
return item; // return the deleted value
}
// Method to search an item in the list. If found, return its
// location, else return -1. It is assumed that the class of item
// implements equals method. The method finds the first
// occurrence of item in the list.
public int indexOf(E item)
{
int index = 0;
Node<E> node = head;
while (node != null)
{
if ( item.equals( node.data) )
return index;
else {
node = node.next;
index++;
}
}
return -1; // item not found
}
// Method to remove the first occurrence of the item from
// the list, if present and return true, else return false
public boolean remove (E item)
{
int index = indexOf(item);
if (index != -1) {
remove(index); // remove node at the index
return true; // item found
}
else
return false; // item not found
}
// Method to check whether an object is in the list. If found,
// return true, else return false.
public boolean contains(E item)
{
int index = indexOf(item);
if (index != -1)
return true; // item found
else
return false; // item not found
}
// Method to delete all nodes from the list and make it empty
public void clear( )
{
while (head != null ){
head.data = null;
head = head.next;
}
size = 0;
}
public String toString( )
{
String str = "";
Node<E> nodeRef = head;
for(int i = 0; i < size; i++) {
str = str + nodeRef.data + " ";
nodeRef = nodeRef.next;
}
return str;
}
public boolean shiftRightRotate()
{
if (size <=1)
return false;
Node<E> node, prev;
node = head.next;
prev = head;
while (node.next != null)
{
node = node.next;
prev = prev.next;
}
E myData = node.data;
Node<E> temp = new Node<E>(myData,head);
head = temp;
prev.next = null;
return true;
}
public E setExampleFromTest(int index, E item) {
if (size == 0 || index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
Node<E> node = head;
for (int i = 0; i < index; i++) {
node = node.next;
}
E myData = node.data;
node.data = item;
return myData;
}
public int countOdd()
{
Node<E> node = head;
int count = 0;
while(node != null)
{
if ((int)node.data%2 == 1)
count++;
node = node.next;
}
return count;
}
public int countOddFor()
{
int count = 0;
for(Node<E> node = head; node != null;node = node.next)
if ((int)node.data%2 == 1)
count++;
return count;
}
//method to be accepts one parameter of type Integer.
// The method prints all elements greater than item and returns their count.
public int printGreater (int item)
{
Node <E> node = head;
int count = 0;
while (node != null)
{
if( (Integer) node.data > item)
{
count++;
System.out.println(node.data);
}
node = node.next;
}
return count;
}
//The method returns true if the first half of the list contains the same
// elements of the second half in the same order.
// If the list does not have even number of elements, so halfs are not equal,
// the method returns false.
public boolean compareHalfs ()
{
Node <E> node1, node2;
node1 = head;
node2 = getNode(size/2);
while (node2 != null)
{
if (node1.data.equals(node2.data))
{
node1 = node1.next;
node2 = node2.next;
}
else return false;
}
return true;
}
//The method removes all the occurrences of item in the list.
public void removeAll(E item)
{
if (size == 0)return;
Node <E> node = head;
for (int i=0; i < size; i++)
{
if((node.data).equals(item))
{
remove(i);
i--;
}
node = node.next;
}
}
}