-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLList.java
301 lines (237 loc) · 6.4 KB
/
SLList.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
import java.util.*;
import java.lang.*;
/**
* This is the SLList class.
* @author Wei Zhong Tee
* @since 8 May 2020
*/
public class SLList <E> implements List <E> {
private Node<E> head; //reference to the head
private int size = 0;
/**
* @param item - the item we are adding to the head of the list
*/
public SLList(E item) {
head = new Node <E> (item);
size++;
}
/**
* Default constructor
*/
public SLList() {
head = null;
size = 0;
}
/** Remove all contents from the list, so it is once again
empty. */
public void clear() {
head = null;
size = 0;
}
public int getSize() {
return size;
}
/** Insert an element at the given location.
* allows you to insert after the tail
* @param item The element to be inserted.
*/
public void insert(int index, E item) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
if (index == 0) { // want a new head
addFirst(item); //helper method
}
else {
Node<E>node = getNode(index - 1); //the node before the index
addAfter(node, item);
}
}
/**
* Helper method that makes a new head
* @param item to imsert into a new head
*/
private void addFirst(E item) {
head = new Node <E> (item, head);
size++;
}
/**
* Helper to add a node after a given node
* @param node to add after
* @param item to add into the node
*/
private void addAfter(Node <E> node, E item) {
node.setNext(new Node <E> (item, node.getNext()));
size++;
}
/**
* helper method to get a node at a given Index
* @param index - the index of the node to retrieve
*/
private Node <E> getNode(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
Node <E> node = head;
for (int i = 0; i < index; i++) {
node = node.getNext();
}
return node;
}
/** Append an element at the end of the list.
* @param item The element to be appended.
*/
public void add(E item) {
if (size == 0) { //nothing was in the list so a new head was made
addFirst(item);
}
else {
Node <E> node = getNode(size - 1);
addAfter(node, item);
}
}
/**
* Remove the element at the given location.
* @param index - index of node to remove
*/
public void remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
if (index == 0) { //removing head
removeFirst(); //helper
}
else {
Node <E> node = getNode (index - 1);
removeAfter(node); //helper
}
}
/**
* Helper to remove the head
*/
private void removeFirst() {
if (head != null) {
head = head.getNext();
size--;
}
}
/**
* Helper to remove after a given node
* @param node the node to remove after
*/
private void removeAfter(Node <E> node) {
Node <E> temp = node.getNext();
if (temp != null) {
node.setNext(temp.getNext());
size--;
}
}
/**
* Get the element in the position to one step left.
* @return element in the node to the left of the node at the index,
* null if at the head.
*/
public E prev(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
if (index == 0) {
return null;
}
else {
return getNode(index-1).getElement();
}
}
/** Get the element in the position one step right.
* @return the element in the node to the right of
* the node at the index, null if at the end.
*/
public E next(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
if (index == size - 1) {
return null;
}
else {
return getNode(index+1).getElement();
}
}
/** @return The number of elements in the list. */
public int length() {
return size;
}
/** Turn the contents of the Nodes to a string in order from head to end.
* @return The String representation of the
* elements in the list from head to end.
*/
public String toString() {
Node<E> nodeRef = head;
String result = "";
while (nodeRef!=null) {
result = result + nodeRef.getElement().toString();
if (nodeRef.getNext() != null) {
result = result + " ==> ";
}
nodeRef = nodeRef.getNext();
}
return result;
}
/** Reverse the content of the list.
* if list is A => B => C it becomes C => B => A
*/
public void reverse() {
Node <E> node = head;
if (node == null || node.getNext() == null) { //empty or one node
return;
}
Node <E> prev = node.getNext();
Node <E> curr = prev.getNext();
prev.setNext(node);
node.setNext(null);
while (curr != null) {
Node <E> next = curr.getNext();
curr.setNext(prev);
prev = curr;
curr = next;
}
head = prev;
}
/** @return The element at given position. */
public E getValue(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
Node <E> node = head ;
for (int i = 0; i < index; i++) {
node = node.getNext();
}
return node.getElement();
}
/**
* insert a list after given index
* @param list the list to be inserted
* @param index the index of where the list should go after
*/
public void insertList (SLList list, int index) {
if ((index < size && index >= 0) && list.getHead() != null) {
Node <E> temp1 = getNode(index);
Node <E> temp2 = temp1.getNext();
Node <E> temp3 = list.getLast();
temp3.setNext(temp2);
temp1.setNext(list.getHead());
size += list.length();
}
}
/** @return the head of the list*/
public Node<E> getHead() {
return head;
}
/** @return the last node in the list*/
public Node<E> getLast() {
return getNode(size - 1);
}
public E get(int index) {
return getValue(index);
}
}