-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedList.java
More file actions
322 lines (240 loc) · 7.31 KB
/
LinkedList.java
File metadata and controls
322 lines (240 loc) · 7.31 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
import java.util.NoSuchElementException;
//basjgbaongoargohaog
/** Implements a linked list. The elements are doubly linked. The list
* is circular and has a dummy node.
*
* @author Marcel Turcotte (turcotte@eecs.uottawa.ca)
*/
public class LinkedList<E> {
// Objects of the class Elem are used to store the elements of the
// list.
private static class Elem<T> {
private final T value;
private Elem<T> previous;
private Elem<T> next;
private Elem(T value, Elem<T> previous, Elem<T> next) {
this.value = value;
this.previous = previous;
this.next = next;
}
}
// An inner (non-static) class is used to implement the interface
// Iterator.
private class LinkedListIterator implements Iterator<E> {
private Elem<E> current;
private LinkedListIterator() {
current = head;
}
public E next() {
if (current.next == head) {
throw new NoSuchElementException();
}
current = current.next ; // move the cursor forward
return current.value ;
}
public boolean hasNext() {
return current.next != head;
}
}
private final Elem<E> head;
private int size;
public LinkedList() {
head = new Elem<E>(null, null, null);
head.next = head;
head.previous = head;
size = 0;
}
/**
* Returns an iterator for this list.
*
* @return an iterator for this list
*/
public Iterator<E> iterator() {
return new LinkedListIterator();
}
/**
* Returns an iterator for this list stopping at a specified position.
*
* @param stop the index of the last element of the iteration
* @return an iterator for this list
*/
public Iterator<E> iterator(E stop) {
LinkedListIterator iteratorA = new LinkedListIterator();
int posA=0;
for (int x=0; x<size();x++){
if(x==0){
addFirst(iteratorA.next());
posA++;
}else{
//E next= next();
if(iteratorA.hasNext()==true && iteratorA.next()!=stop){
add(posA, iteratorA.next());
posA++;
}else if(iteratorA.next()==stop){
addLast(stop);
}
}
}
return iteratorA;
}
/**
* Returns an iterator for this list that starts at a specified
* position and stops at a specified position.
*
* @param start the index of the first element of the iteration
* @param stop the index of the last element of the iteration
* @return an iterator for this list
*/
public Iterator<E> iterator(E start, E stop) {
LinkedListIterator iteratorB = new LinkedListIterator();
int posB=0;
int count=0;
for (int y=0; y<size();y++){
if(count==0){
if(iteratorB.next()==start){
addFirst(iteratorB.next());
posB++;
}else{
count=0;
}
}else{
//E next= next();
if(iteratorB.hasNext()==true && iteratorB.next()!=stop){
add(posB, iteratorB.next());
posB++;
}else if(iteratorB.next()==stop){
addLast(stop);
}
}
}
return iteratorB;
}
/** Returns the size of the list.
*
* @return the size of the list
*/
public int size() {
return size;
}
// Helper method. Adds an element to the list after the specified
// node.
private void addAfter(Elem<E> before, E obj) {
Elem<E> after = before.next;
before.next = new Elem<E>(obj, before, after);
after.previous = before.next;
size++;
}
/** Inserts the specified element at the beginning of this list.
*
* @param obj the object to be added
*/
public void addFirst(E obj) {
if (obj == null) {
throw new NullPointerException();
}
addAfter(head, obj);
}
/** Inserts the specified element at the end of this list.
*
* @param obj the object to be added
*/
public void addLast(E obj) {
if (obj == null) {
throw new NullPointerException();
}
addAfter(head.previous, obj);
}
/** Inserts the specified element at a specified position of this list.
*
* @param pos the specified position
* @param obj the object to be added
* @throws IndexOutOfBoundsException if the specified position is out of range
*/
public void add(int pos, E obj) {
if (obj == null) {
throw new NullPointerException();
}
if (pos < 0 || pos > size) {
throw new IndexOutOfBoundsException(Integer.toString(pos));
}
Elem<E> before;
before = head;
for (int i=0; i<pos; i++) {
before = before.next;
}
addAfter(before, obj);
}
// Helper method. Removes the specified node.
private void remove(Elem<E> current) {
Elem<E> before = current.previous, after = current.next;
before.next = after;
after.previous = before;
size--;
}
/** Removes the first element from this list.
*/
public void removeFirst() {
if (size == 0) {
throw new NoSuchElementException();
}
remove(head.next);
}
/** Removes the last element from this list.
*/
public void removeLast() {
if (size == 0) {
throw new NoSuchElementException();
}
remove(head.previous);
}
/** Remove the element at the specified position.
*
* @param pos the specified position
* @throws IndexOutOfBoundsException if the specified position is out of range
*/
public void remove(int pos) {
if (pos < 0 || pos >= size) {
throw new IndexOutOfBoundsException(Integer.toString(pos));
}
Elem<E> current;
current = head.next;
for (int i=0; i<pos; i++) {
current = current.next;
}
remove(current);
}
/** Returns the element found at the specied position.
*
* @param pos the specified position
* @return the element found at the specified position
* @throws IndexOutOfBoundsException if the specified position is out of range
*/
public E get(int pos) {
if (pos < 0 || pos >= size) {
throw new IndexOutOfBoundsException(Integer.toString(pos));
}
Elem<E> current;
current = head.next;
for (int i=0; i<pos; i++) {
current = current.next;
}
return current.value;
}
/** Returns a String representation of this list.
*
* @return a String representation of this list
*/
public String toString() {
StringBuffer str = new StringBuffer("{");
Elem<E> p = head.next;
while (p != head) {
str.append(p.value);
if (p.next != head) {
str.append(",");
}
p = p.next;
}
str.append("}");
return str.toString();
}
}