-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.java
More file actions
298 lines (274 loc) · 8.58 KB
/
ArrayList.java
File metadata and controls
298 lines (274 loc) · 8.58 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
/**
* Write a description of class Quack here.
*
* @author
* @version
*/
public class ArrayList <T>{
public class Node {
public T data; // the data stored in this node
public Node next; // a reference to the next node in the list
/**
* Default no-arg constructor
* pre: none
* post: data and next are null.
*/
public Node() {}
/**
* Full-arg constructor
* pre: none
* post: data and next are as per the param
*/
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
/**
* Only data constructor
* pre: none
* post: data is as per param and next is null.
*/
public Node(T data) {
this.data = data;
this.next = null;
}
} // end of inner class
public Node head = null;
/**
* This method adds the param element to the end of the list
* pre: none, but makes no sense to add null data
* post: adds another node to the linked list at the end. the definition of end is simply
* the last node in a chain of nodes.
*/
protected void append(T newData) {
Node newNode = new Node(newData);
if (isEmpty()) { // if list is empty, head is the newdata.
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}
/**
* This method adds the param element to the end of the list
* pre: none, but makes no sense to add null data
* post: adds another node to the linked list at the end. the definition of end is simply
* the last node in a chain of nodes.
*/
protected void add(T newData) {
Node newNode = new Node(newData);
if (isEmpty()) { // if list is empty, head is the newdata.
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}
/**
* this method removes an element at a spesificied index.
* pre: none
* post: returns type objecty with the object removed from the param index.
*/
protected T remove(int index) {
// out of bounds case
if (index < 0 || index >= this.size()) {
System.out.println("Index out of bounds. You get a null return now :(.");
return null;
}
// null array case
if (this.isEmpty()) return null;
// finding and linking the previous and next node together (of the target node)
Node current = head;
T toRemove;
if (index == 0) { // if removing the head
toRemove = current.data;
head = current.next;
} else { // all other cases.
// chat gpt has helped with a promt along the lines of write some code that iterates
//through a list of nodes.
Node previous = null;
for (int i = 0; i < index; i++) {
previous = current;
current = current.next;
}
toRemove = current.data;
previous.next = current.next;
}
return toRemove;
}
/**
* This method adds another element at the specified index and everything is moved accordingly
* pre: Object and int param respectively.
* post: adds a new node at the specified index linking the following nodes to the new one.
*/
protected void insert(T newData, int index) {
if (index < 0 || index > size()) {
System.out.println("The index is out of bounds.");
return;
}
Node newNode = new Node(newData);
if (index == 0) {
newNode.next = head;
head = newNode;
} else {
Node current = head;
Node previous = null;
int currentIndex = 0;
while (currentIndex < index) {
previous = current;
current = current.next;
currentIndex++;
}
previous.next = newNode;
newNode.next = current;
}
}
/**
* This method deletes the element at the specified index.
* pre: type int param
* post: deletes the element at the param.
*/
protected void delete(int index) {
if (index < 0 || index >= size()) {
System.out.println("Invalid index for deletion.");
return;
}
if (index == 0) { // Deleting the first element
head = head.next;
} else {
Node current = head;
Node previous = null;
int currentIndex = 0;
while (currentIndex < index) {
previous = current;
current = current.next;
currentIndex++;
}
previous.next = current.next;
}
}
/**
* this method clears the array.
* pre: none
* post: the array is now empty, it is null.
*/
public void clear() {
this.remove(0);
this.head = null;
}
/**
* This method returns the element at the specified index
* pre: type int param
* post: returns type Object with the element at the index
*/
protected T get(int index) {
if (index < 0 || index >= size()) {
System.out.println("Invalid index.");
return null;
}
return getHelper(head, index).data;
}
/**
* Helper method that gets the node at the index value and returns that node.
*/
private Node getHelper(Node front, int index) {
if (index == 0) {
return front;
}
return getHelper(front.next, index - 1);
}
/**
* This method returns the size of the linked list recursively using a helper method
* pre: none
* post: returns type int with the number of nodes.
*/
public int size() {
return sizeHelper(head);
}
/**
* Private helper method to recursively find the length / size of the linked list
*/
private int sizeHelper(Node front) {
if (front == null) {
return 0;
}
return 1 + sizeHelper(front.next);
}
/**
* This method returns a string with all of the data values of the nodes
* pre: none
* post: returns type String with the data values of the nodes.
*/
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Data: [");
Node current = head;
while (current != null) {
stringBuilder.append(current.data);
if (current.next != null) {
stringBuilder.append(", ");
}
current = current.next;
}
stringBuilder.append("]");
return stringBuilder.toString();
}
/**
* This method checks if the first node is null or not which means if it is empty or not
* pre: none
* post: returns type boolean indicating if it is empty
*/
public boolean isEmpty() {
return head == null;
}
/**
* Returns the index of an object in the linked list, and returns -1 if it does not exist
* pre: object type param indicating the element you want the index of
* post: returns with int of either the index number or -1 (not found)
*/
protected int indexOf(T target) {
Node current = head;
int index = 0;
while (current != null) {
if (current.data.equals(target)) {
return index;
}
current = current.next;
index++;
}
return -1;
}
/**
* Override this method
* - I am pretty sure that wh
* This method should determine whether two of the same class type objects are the same.
*/
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ArrayList)) {
return false;
}
ArrayList that = (ArrayList) other;
if (this.size() != that.size()) {
return false;
}
Node thisNode = this.head;
Node thatNode = that.head;
while (thisNode != null) {
if (!thisNode.data.equals(thatNode.data)) {
return false;
}
thisNode = thisNode.next;
thatNode = thatNode.next;
}
return true;
}
}