-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathShingleLinkedList.java
More file actions
285 lines (257 loc) · 6.67 KB
/
ShingleLinkedList.java
File metadata and controls
285 lines (257 loc) · 6.67 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
package com.linkedlist;
import java.util.Iterator;
/**
* Shingle LinkedList implementation of the LinkedList API.
*
* @author STEPHANE MIGUEL KAKANAKOU (Skakanakou@gmail.com)
*
* @param <Item>
*/
public class ShingleLinkedList<Item> implements LinkedList<Item> {
/*
* Structure of each node of the LinkedList.
*/
private class Node {
Item item;
Node next;
private Node(Item item) {
this.item = item;
}
}
private Node head;
private Node tail;
private int size;
public ShingleLinkedList() {
head = null;
tail = null;
size = 0;
}
/*** {@inheritDoc} */
@Override
public int size() {
return size;
}
/*** {@inheritDoc} */
@Override
public boolean empty() {
return head == null;
}
/*** {@inheritDoc} */
@Override
public Item value_at(int index) {
if (empty())
throw new NullPointerException("Empty List");
if ((index < 0) || (index >= size))
throw new IllegalArgumentException("Illegal index");
Node x = head;
for (int i = 0; i < index; i++) {
x = x.next;
}
return x.item;
}
/*** {@inheritDoc} */
@Override
public void push_front(Item item) {
if (item == null)
throw new IllegalArgumentException("Null Item");
Node x = new Node(item);
x.next = head;
head = x;
if (tail == null) {
tail = x;
}
size++;
}
/*** {@inheritDoc} */
@Override
public Item pop_front() {
if (head == null)
throw new NullPointerException("Empty List");
Node x = head;
head = head.next;
if (head == null)
tail = null;
size--;
return x.item;
}
/*** {@inheritDoc} */
@Override
public void push_back(Item item) {
if (item == null)
throw new IllegalArgumentException("Null Item");
Node x = new Node(item);
x.next = null;
if (tail == null) {
head = x;
tail = x;
} else {
tail.next = x;
tail = x;
}
size++;
}
/*** {@inheritDoc} */
@Override
public Item pop_back() {
if (head == null)
throw new NullPointerException("Empty List");
Node x = tail;
if (head == tail) {
head = null;
tail = null;
} else {
Node node = head;
while (node.next.next != null) {
node = node.next;
}
node.next = null;
tail = node;
}
size--;
return x.item;
}
/*** {@inheritDoc} */
@Override
public Item front() {
if (head == null)
throw new NullPointerException("Empty List");
return head.item;
}
/*** {@inheritDoc} */
@Override
public Item back() {
if (tail == null)
throw new NullPointerException("Empty List");
return tail.item;
}
/*** {@inheritDoc} */
@Override
public void insert(int index, Item item) {
if ((index < 0) || (index > size))
throw new IllegalArgumentException("Illegal index");
if (item == null)
throw new IllegalArgumentException("Null item");
if (index == 0) {
this.push_front(item);
} else if (index == size) {
this.push_back(item);
} else {
Node node = head;
for (int i = 0; i < index - 1; i++) {
node = node.next;
}
Node x = new Node(item);
x.next = node.next;
node.next = x;
size++;
}
}
/*** {@inheritDoc} */
@Override
public void erase(int index) {
if ((index < 0) || (index >= size))
throw new IllegalArgumentException("Illegal index");
if (index == 0) {
this.pop_front();
} else if (index == size - 1) {
this.pop_back();
} else {
Node node = head;
for (int i = 0; i < index - 1; i++) {
node = node.next;
}
node.next = node.next.next;
size--;
}
}
/*** {@inheritDoc} */
@Override
public Item value_n_from_end(int backIndex) {
if ((backIndex < 0) || (backIndex >= size))
throw new IllegalArgumentException("Illegal index");
if (backIndex == 0) {
return this.back();
} else if (backIndex == size - 1) {
return this.front();
} else {
Node node = head;
for (int i = 0; i < backIndex; i++) {
node = node.next;
}
Node x = head;
while (node != tail) {
node = node.next;
x = x.next;
}
return x.item;
}
}
/*** {@inheritDoc} */
@Override
public void reverse() {
if (head == null)
return;
tail = head;
reverse(head);
}
private void reverse(Node x) {
if (x.next == null) {
head = x;
return;
}
reverse(x.next);
x.next.next = x;
x.next = null;
}
/*** {@inheritDoc} */
@Override
public void remove_value(Item item) {
if (item == null)
throw new IllegalArgumentException("Null item");
if (item.equals(head.item)) {
this.pop_front();
return;
}
Node x = head;
while ((x.next != null) && (!x.next.item.equals(item))) {
x = x.next;
}
if (x.next != null) {
if (x.next == tail) {
x.next = x.next.next;
tail = x;
} else {
x.next = x.next.next;
}
size--;
}
}
/*** {@inheritDoc} */
@Override
public Iterator<Item> iterator() {
return new ShingleLinkedListIterator();
}
/**
* ShingleLinkedListIterator implements Iterator interface in order to
* provide iterable capabilities to the list.
*
* @author STEPHANE MIGUEL KAKANAKOU (Skakanakou@gmail.com)
*
*/
private class ShingleLinkedListIterator implements Iterator<Item> {
private Node current;
public ShingleLinkedListIterator() {
current = head;
}
@Override
public boolean hasNext() {
return current != null;
}
@Override
public Item next() {
Item item = current.item;
current = current.next;
return item;
}
}
}