-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleList.java
More file actions
204 lines (163 loc) · 4.05 KB
/
DoubleList.java
File metadata and controls
204 lines (163 loc) · 4.05 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
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Doubly-linked list class (UNFINISHED).
*
* @author Maricel Vicente
*
*/
public class DoubleList<E> implements Iterable<E> {
private Link<E> head; // Pointer to list header
private Link<E> tail; // Pointer to last node
private int listSize; // Size of list
/**
* Create an empty LList.
*/
DoubleList() {
clear();
}
/**
* Remove all elements.
*/
public void clear() {
tail = new Link<E>(null, null); // Create trailer
head = new Link<E>(null, tail); // Create header
tail.setPrev(head);
listSize = 0;
}
/**
* Append item to the end of the list.
*/
public void append(E item) {
Link<E> newLink = new Link<>(item, tail.prev(), tail);
tail.prev().setNext(newLink);
tail.setPrev(newLink);
listSize++;
}
/**
* Return the element at the provided index. This method will iterate from the
* head or the tail depending on which will require fewer steps.
*/
public E get(int pos) {
if (pos < 0 || pos >= listSize) {
throw new IndexOutOfBoundsException();
}
if (pos < listSize / 2) {
return forward(pos).element();
}
else {
return backward(pos).element();
}
}
/**
* Helper method for iterating forward from the head.
*/
private Link<E> forward(int pos) {
Link<E> current = head.next();
for (int i = 0; i < pos; i++) {
current = current.next();
}
return current;
}
/**
* Helper method for iterating backward from the tail.
*/
private Link<E> backward(int pos) {
Link<E> current = tail.prev();
for (int i = 0; i < (listSize - 1) - pos; i++) {
current = current.prev();
}
return current;
}
/**
* Return the number of elements stored in the list.
*/
public int size() {
return listSize;
}
/**
* Iterates forward through the list. Remove operation is supported.
*/
@Override
public Iterator<E> iterator() {
return new DoubleIterator();
}
private class DoubleIterator implements Iterator<E> {
private Link<E> current = head;
@Override
public boolean hasNext() {
return (current.next().next() != null);
}
@Override
public E next() {
current = current.next();
E el = current.element();
return el;
}
@Override
public void remove() {
Link<E> previous = current.prev();
Link<E> next = current.next();
previous.setNext(next);
next.setPrev(previous);
current = previous;
listSize--;
}
}
/**
* Add the item at the specified index.
*/
public void add(int index, E item) {
//COMPLETE THIS
if (index < 0 || index >= listSize) {
throw new IndexOutOfBoundsException();
}
Link<E> current = head.next();
for (int i = 0; i < index; i++) {
current = current.next();
}
Link<E> before = current.prev();
Link<E> body = new Link<>(item, current.prev(), current);
before.setNext(body);
body.setPrev(before);
current.setPrev(body);
body.setNext(current);
listSize++;
}
/**
* Remove and return the item at the specified index.
*/
public E remove(int index) {
//COMPLETE THIS
if (index >= listSize || index < 0) {
throw new IndexOutOfBoundsException();
}
Link<E> current = head.next();
for (int i = 0; i < index; i++) {
current = current.next();
}
E el = current.element();
Link<E> before = current.prev();
Link<E> after = current.next();
before.setNext(after);
after.setPrev(before);
listSize--;
return el;
}
/**
* Reverse the list (in place)
*/
public void reverse() {
//COMPLETE THIS
Link<E> reverseNext = head.next();
Link<E> reversePrev = tail.prev();
for(int i=0; i< listSize / 2; i++)
{
E el = reverseNext.element();
reverseNext.setElement(reversePrev.element());
reversePrev.setElement(el);
reverseNext = reverseNext.next();
reversePrev = reversePrev.prev();
}
}
}