-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleList.java
237 lines (177 loc) · 5.43 KB
/
DoubleList.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
/**
* @author Cameron Maxwell
* class represents a doubly linked list of nodes of the class DoubleNode
* @param <T>
*/
public class DoubleList<T> {
// reference to first node in the list
private DoubleNode<T> head;
// reference to last node in the list
private DoubleNode<T> rear;
// number of nodes in the list
private int numDataItems;
public DoubleList() {
head = null;
rear = null;
numDataItems = 0;
}
/**
* @param index
* @param newData
* @throws InvalidPositionException
*/
public void addData(int index, T newData) throws InvalidPositionException {
// create newNode and CurrentDoubleNode
DoubleNode<T> newNode = new DoubleNode<T>(newData);
DoubleNode<T> currentDoubleNode = head;
if (index > numDataItems || index < 0) {
throw new InvalidPositionException("Invalid index");
}
// case 1: empty list and adds data
if (head == null) {
// the head and rear must become new node
head = newNode;
rear = newNode;
numDataItems ++;
return;
}
// case 2: 1 item in list and adds data
else if (index == 0) {
// make the current node the next node
currentDoubleNode = head;
// make the newNode the front
head = newNode;
head.setNext(currentDoubleNode);
numDataItems ++;
return;
}
// case 3: specified index is at the end of the list
else if (index == numDataItems) {
// make the rear the current node
currentDoubleNode = rear;
// make the newNode the rear
rear = newNode;
// set current's next to the newNode (rear)
currentDoubleNode.setNext(rear);
rear.setNext(head);
// increment numDataItems count
numDataItems ++;
return;
}
// case 4: checks if index anywhere between and adds data
for (int x = 0; x < numDataItems; x++) {
// if list is empty, make the newNode the head and the rear as its only element in now
if (index == 0 && numDataItems ==0) {
head = newNode;
rear = newNode;
}
// once it gets to the desired node specified by the index
else if (index == x) {
DoubleNode<T> previous = head;
DoubleNode<T> successor = head.getNext();
// set previous' next to the new node
previous.setNext(newNode);
// set (previous' next)'s next to the successor
previous.getNext().setNext(successor);
// increment numDataItems count
numDataItems ++;
return;
}
// iterate through by getting the next node if no return (specified index not reached yet)
currentDoubleNode = currentDoubleNode.getNext();
} // for loop closes
numDataItems ++;
} // method closes
/**
* @param index
* @return
* @throws InvalidPositionException
*/
public DoubleNode<T> getNode(int index) throws InvalidPositionException {
if (index < 0 || index >= numDataItems) {
throw new InvalidPositionException("Invalid position.");
}
// case 1: if front of list
if (index == 0) {
return head;
}
// case 2: if at end of list
if (index == numDataItems - 1) {
return rear;
}
// case 3: if somewhere between in list
DoubleNode<T> currentNode = head;
int ctr = 0;
while (ctr != index) {
// until reach index, get the next node in list
currentNode = currentNode.getNext();
ctr ++;
}
// return the node once it hits the given index
return currentNode;
} // method ends
/**
* @param index
* @throws InvalidPositionException
*/
public void removeData(int index) throws InvalidPositionException {
if (index < 0 || index >= numDataItems) {
throw new InvalidPositionException("Position is invalid.");
}
// case 1: if there are no nodes to delete
if (head == null) {
return;
}
// case 2: if node to delete is at front (remove the head)
if (index == 0) {
// removes the reference to the first link (head)
head = head.getNext();
return;
}
// case 3: if node to delete is at end
if (index == numDataItems - 1) {
DoubleNode<T> previousNode = head;
DoubleNode<T> nodeToDelete = head.getNext();
while (nodeToDelete.getNext() != null) {
// iterate through till the next is null which proves the end of the list
previousNode = nodeToDelete;
nodeToDelete = nodeToDelete.getNext();
}
// next must be null
previousNode.setNext(null);
return;
}
// case 4: otherwise where the node to delete is in the middle
DoubleNode<T> previousNode = this.getNode(index -1);
DoubleNode<T> nodeToDelete = this.getNode(index);
previousNode.setNext(nodeToDelete.getNext());
// reduce the count of data items
numDataItems--;
} // method closes
/**
* @param index
* @return
* @throws InvalidPositionException
*/
public T getData(int index) throws InvalidPositionException {
if (index < 0 || index >= numDataItems) {
throw new InvalidPositionException("Invalid index.");
}
// get the node given an index, and get it's data using getData() from DoubleNode
return getNode(index).getData();
} // method ends
/**
* @param index
* @param newData
* @throws InvalidPositionException
*/
public void setData(int index, T newData) throws InvalidPositionException{
if (index < 0 || index >= numDataItems) {
throw new InvalidPositionException("Invalid index.");
}
// create newNode which is the node at the given index in the list
DoubleNode<T> newNode = getNode(index);
// set the newNode's data given the new data
newNode.setData(newData);
} // method closes
} // class closes