-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLinkedList.java
340 lines (336 loc) · 7.49 KB
/
LinkedList.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
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
335
336
337
338
339
340
public class LinkedList
{
private ListNode head;
private int length;
//CONSTRUCTORS
public LinkedList()
{
length = 0;
}
//GETTERS AND SETTERS FOR HEAD NODE
public ListNode getHead()
{
return head;
}
public void setHead(ListNode head)
{
this.head = head;
}
/* ---------------------
* --------METHODS------
* INSERTION
* 1. AT BEGINNING
* 2. AT END
* 3. AT MIDDLE
* DELETION
* 1. FROM BEGINNING
* 2. FROM END
* 3A FROM MIDDLE USING INDEX
* 3B FROM MIDDLE USING VALUE
* TOSTRING
* LENGTH
* SEARCH ELEMENT
* OPTIMIZED SEARCH
* CLEAR LIST
* PRINT LIST
*/
//INSERTION
//1. AT BEGINNING
public synchronized void insertAtBegin(int value)
{
ListNode newNode = new ListNode(value);
if(head==null)
{
head = newNode;
}
else
{
newNode.setNext(head);
head=newNode;
}
length++;
}
//2. AT END
public synchronized void insertAtEnd(int value)
{
ListNode newNode = new ListNode(value);
if(head==null)
{
head=newNode;
}
else
{
ListNode current = head;
while(current.getNext()!=null)
{
current = current.getNext();
}
// System.out.println(current.getObject());
current.setNext(newNode);
}
length++;
}
//3. AT MIDDLE
public void insert(int data,int position)
{
ListNode newNode = new ListNode(data);
//fix the position
if(position<0)
{
position = 0;
}
if(position > length)
{
position = length;
}
//if list is empty
if(head==null)
{
head = newNode;
}
else if(position == 0) //added at 0
{
newNode.setNext(head);
head=newNode;
}
else
{
ListNode current=head;
for(int i=1;i<length;i++)
{
if(i==position)
{
break;
}
current = current.getNext();
}
newNode.setNext(current.getNext());
current.setNext(newNode);
}
length++;
}
//DELETION
//1. FROM HEAD
public synchronized ListNode removeFromBegin()
{
if(head==null)
{
return null;
}
else if(head.getNext() == null)
{
ListNode temp = head;
head=null;
length--;
return temp;
}
else
{
ListNode temp = head;
head = head.getNext();
length--;
return temp;
}
}
//2. FROM END
public synchronized ListNode removeFromEnd()
{
if(head==null)
{
return null;
}
else if(head.getNext() == null)
{
ListNode temp = head;
head = null;
length--;
return temp;
}
else
{
ListNode current=head;
ListNode prev=head;
while(current.getNext() != null)
{
prev=current;
current=current.getNext();
}
prev.setNext(null);
length--;
return current;
}
}
//3A. FROM MIDDLE USING INDEX
public synchronized void remove(int position)
{
//fix position
if(position<0)
{
position=0;
}
else if(position>=length)
{
position = length-1;
}
if(head==null)
{
return;
}
else if(position==0) //deletion from head
{
ListNode temp=head;
head=head.getNext();
temp.setNext(null);
}
else
{
ListNode current = head;
ListNode prev=null;
for(int i=0;i<position;i++)
{
prev=current;
current=current.getNext();
}
prev.setNext(current.getNext());
}
length--;
}
//3B. FROM MIDDLE USING VALUE
public synchronized void removeMatched(int value)
{
if(head==null)
{
return;
}
else if(head.getData()==value)
{
ListNode temp = head;
head=head.getNext();
temp.setNext(null);
length--;
}
else
{
ListNode current=head;
ListNode prev = head;
while(current.getNext()!=null)
{
if(current.getData() == value)
{
break;
}
prev=current;
current=current.getNext();
}
prev.setNext(current.getNext());
length--;
}
}
//TO STRING METHOD FOR TYPE CONVERSION
//FORMAT: [1,2,3,4,5]
public String toString()
{
String result="[";
if(head==null)
{
return result+"]";
}
ListNode current = head;
while(current.getNext() != null)
{
result=result+current.toString()+",";
current = current.getNext();
}
return result+current.toString()+"]";
}
//length of linked list
public int length()
{
return length;
}
//SEARCH LINKED LIST
public boolean search_element(int find)
{
if(head==null)
{
return false;
}
ListNode current=head;
int flag=0;
while(current!=null)
{
if(current.getData()==find)
{
flag=1;
break;
}
current=current.getNext();
}
if(flag==1)
{
return true;
}
return false;
}
//OPTIMIZED SEARCH
public boolean optimized_search(int find)
{
if(head==null)
{
return false;
}
ListNode current1=head;
ListNode current2=head.getNext();
int flag=0;
while(true)
{
// System.out.println("current1 "+current1);
// System.out.println("current2 "+current2);
if(current1!=null)
{
if(current1.getData()==find)
{
flag=1;
break;
}
}
else if(current2!=null)
{
if(current2.getData()==find)
{
flag=1;
break;
}
}
if(current2==null || current2.getNext()==null || current1==null || current1.getNext()==null)
{
break;
}
current2=current2.getNext().getNext();
current1=current1.getNext().getNext();
}
if(flag==1)
{
return true;
}
return false;
}
//CLEAR LIST
public void clearList()
{
head=null;
length=0;
}
//PRINT LIST
//FORMAT: 1->2->3->null
public void print_list()
{
ListNode current = head;
while(current != null)
{
System.out.print(current + "->");
current = current.getNext();
}
System.out.println("null");
}
}