-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularlyLinkedList.java
397 lines (328 loc) · 6.51 KB
/
CircularlyLinkedList.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package projectCode20280;
/*
The CircularlyLinkedList class implements the List<T> abstract class.
The CircularlyLinkedList is a type of linked list that has no end,
what would be the end simply links to what would be the start of the list.
It implements an inner Node<E> class to hold the data.
A tail pointer is used to keep track of the connect between the end and the start of the circle.
This tail also allows for a rotate() method, meaning we can change the entry point for the list.
*/
import java.util.Iterator;
public class CircularlyLinkedList<E> implements List<E> {
Node<E> tail = null;
int size = 0;
private class Node<E> {
private E element;
private Node<E> next;
public Node(E e, Node<E> next)
{
this.element = e;
this.next = next;
}
public E getElement()
{
return element;
}
public Node<E> getNext()
{
return next;
}
public void setNext(Node<E> n)
{
next = n;
}
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
if (tail == null)
{
return true;
}
else
{
return false;
}
}
/**
* Returns, but does not remove, the element stored at position i in the list.
*
* @param i Position of element to return
*/
@Override
public E get(int i) {
if (tail == null || (i > size-1))
{
throw new RuntimeException("cannot get");
}
Node<E> curr = tail.getNext();
//start at tail, walk 'i' steps
for (int k = 0; k < i; k++)
{
curr = curr.getNext();
}
return curr.element;
}
/**
* Add an element at the given position in the list
*
* @param i Position to add the specified element
* @param e element to be added
*/
@Override
public void add(int i, E e) {
if (tail == null || i > (size-1))
{
throw new RuntimeException("cannot add");
}
//used to walk 'i' steps
Node<E> curr = tail.getNext();
Node<E> prev = null;
for (int k = 0; k < i; k++)
{
prev = curr;
curr = curr.getNext();
}
//insert newest element (node)
Node<E> newest = new Node<E>(e, curr);
prev.setNext(newest);
size++;
}
/**
* Removes and returns the element at position i in the list.
*
* @param i Position of element to be remove
* @return Element removed
*/
@Override
public E remove(int i) {
if (tail == null)
{
throw new RuntimeException("cannot delete");
}
Node<E> curr = tail.getNext();
Node<E> prev = null;
for (int k = 0; k < i; k++)
{
prev = curr;
curr = curr.getNext();
}
//if element at i is null, or reach end of list before i
if (curr == null)
{
throw new RuntimeException("cannot delete");
}
E e = prev.element;
prev.next = curr.next;
size--;
return e;
}
/**
* Removes and returns the first element in the list
*
* @return Element that was at the front of the list
*/
@Override
public E removeFirst() {
if (size == 0)
{
throw new RuntimeException("cannot remove");
}
if (size == 1)
{
E e = tail.getElement();
tail = null;
size--;
return e;
}
else
{
E e = tail.getNext().getElement();
//skip pointer over first element
tail.setNext(tail.getNext().getNext());
size--;
return e;
}
}
/**
* Removes and returns the last element in the list
*
* @return Element that was last in the list
*/
@Override
public E removeLast() {
if (size == 0)
{
throw new RuntimeException("cannot remove");
}
if (size == 1)
{
E e = tail.getElement();
tail = null;
size--;
return e;
}
else
{
Node<E> find = tail;
//need to find node to reconnect to new tail
while (find.getNext() != tail)
{
find = find.getNext();
}
E e = tail.getElement();
find.setNext(tail.getNext());
tail = find;
size--;
return e;
}
}
private class ListIterator implements Iterator<E>
{
Node<E> curr;
boolean first = true;
public ListIterator()
{
curr = tail.getNext();
}
@Override
public boolean hasNext()
{
if (first)
{
first = false;
return true;
}
else
{
return curr != tail.getNext();
}
}
@Override
public E next()
{
E res = (E) curr.getElement();
curr = curr.getNext();
return res;
}
}
@Override
public Iterator<E> iterator() {
return new ListIterator();
}
/**
* Adds and element to the start of the list
*
* @param e Element to be added
*/
@Override
public void addFirst(E e) {
Node<E> newest = new Node<E>(e, null);
if (tail == null)
{
tail = newest;
tail.setNext(tail);
}
else
{
Node<E> find = tail;
//find node to connect to new node that is now tail
for (int k = 0; k < size-1; k++)
{
find = find.getNext();
}
find.setNext(newest);
newest.setNext(tail);
}
size++;
}
/**
* Adds element to the end of the list
*
* @param e Element to be added
*/
@Override
public void addLast(E e) {
if (tail == null)
{
Node<E> newest = new Node<E>(e, null);
tail = newest;
tail.setNext(tail);
}
else
{
Node<E> newest = new Node<E>(e, null);
newest.setNext(tail.getNext());
tail.setNext(newest);
tail = newest;
}
size++;
}
/**
* Return first element of list
*
* @return first element of list
*/
public E first()
{
if (this.isEmpty())
{
return null;
}
else {
return tail.getNext().getElement();
}
}
/**
* Returns last element of list
*
* @return Last element of list
*/
public E last()
{
return tail.getElement();
}
//make new entry point in list
public void rotate() {
tail = tail.getNext();
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
Node<E> walk = tail.getNext();
do
{
sb.append(walk.getElement());
sb.append(", ");
walk = walk.getNext();
} while (walk != tail.getNext());
sb.replace(sb.length()-2, sb.length(), "");
sb.append("]");
return sb.toString();
}
public static void main(String[] args) {
CircularlyLinkedList<Integer> ll = new CircularlyLinkedList<Integer>();
for(int i = 10; i < 20; ++i) {
ll.addLast(i);
}
System.out.println("Initial:\n" + ll);
ll.removeFirst();
System.out.println("Removed First:\n" + ll);
ll.removeLast();
System.out.println("Removed Last:\n" + ll);
ll.rotate();
System.out.println("Rotated:\n" + ll);
ll.removeFirst();
ll.rotate();
System.out.println("Removed First and Rotated:\n" + ll);
ll.removeLast();
ll.rotate();
System.out.println("Removed Last and Rotated:\n" + ll);
for (Integer e : ll) {
System.out.println("value: " + e);
}
}
}