-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.java
More file actions
346 lines (308 loc) · 8.03 KB
/
Copy pathList.java
File metadata and controls
346 lines (308 loc) · 8.03 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* ***************************************************
* Michael Krueger
* 09/03/2024
*
* List Class - handles any form of data
*************************************************** */
public class List<Type>
{
// We don't actually have to set a max size with linked lists
// But it is a good idea.
// Just picture an infinite loop adding to the list! :O
public static final int MAX_SIZE = 50;
private Node<Type> head;
private Node<Type> tail;
private Node<Type> curr;
private int num_items;
// constructor
// remember that an empty list has a "size" of -1 and its "position" is at -1
public List()
{
this.head = null;
this.tail = null;
this.curr = null;
this.num_items = 0;
}
// copy constructor
// clones the list l and sets the last element as the current
public List(List<Type> l)
{
Node<Type> n = l.head;
this.head = this.tail = this.curr = null;
this.num_items = 0;
while (n != null)
{
this.InsertAfter(n.getData());
n = n.getLink();
}
}
// navigates to the beginning of the list
//* */
public void First()
{
this.curr = this.head;
}
// navigates to the end of the list
// the end of the list is at the last valid item in the list
//* */
public void Last()
{
this.curr = this.tail;
}
// navigates to the specified element (0-index)
// this should not be possible for an empty list
// this should not be possible for invalid positions
//* */
public void SetPos(int pos)
{
this.curr = this.head;
if(pos < 0 || pos >= this.num_items)
{
return;
}
for(int i = 0; i < pos; i++)
{
this.curr = this.curr.getLink();
}
}
// navigates to the previous element
// this should not be possible for an empty list
// there should be no wrap-around
//* */
public void Prev()
{
if (this.curr == this.head || this.curr == null)
{
return; // curr is already at the first element or the list is empty
}
Node<Type> temp = this.head;
while(temp != null && temp.getLink() != this.curr)
{
temp = temp.getLink();
}
this.curr = temp;
}
// navigates to the next element
// this should not be possible for an empty list
// there should be no wrap-around
//* */
public void Next()
{
if (this.curr != null && this.curr != this.tail)
{
this.curr = this.curr.getLink();
}
}
// returns the value of the current element at the position
public Type GetValueAt(int pos){
this.SetPos(pos);
return this.GetValue();
}
// returns the location of the current element (or -1)
public int GetPos()
{
Node<Type> temp = this.head;
for(int counter = 0; counter < num_items; counter++)
{
if(temp == this.curr)
{
return counter;
}
temp = temp.getLink();
}
return -1; // Moved outside the loop
}
// returns the value of the current element (or -1)
public Type GetValue()
{
if(this.curr == null)
{
return null;
}
else
{
return this.curr.getData();
}
}
// returns the size of the list
// size does not imply capacity
public int GetSize()
{
return this.num_items;
}
// inserts an item before the current element
// the new element becomes the current
// this should not be possible for a full list
//** */
public void InsertBefore(Type data)
{
Node<Type> n = new Node<Type>(data);
if(this.head == null)
{
this.head = n;
this.tail = n;
this.curr = n;
}
else
{
if(this.curr == this.head)
{
n.setLink(this.head);
this.head = n;
}
else
{
Node<Type> temp = this.head;
while(temp.getLink() != this.curr)
{
temp = temp.getLink();
}
n.setLink(this.curr);
temp.setLink(n);
}
}
this.num_items++;
}
// inserts an item after the current element
// the new element becomes the current
// this should not be possible for a full list
public void InsertAfter(Type data)
{
//Creating the new node//
Node<Type> n = new Node<Type>();
n.setData(data);
if (this.curr == null) {
this.head = this.tail = this.curr = n;
}
else{
n.setLink(this.curr.getLink());
this.curr.setLink(n);
if (this.curr == this.tail){
this.tail = n;
}
this.curr = n;
}
this.num_items += 1;
}
// removes the current element
// this should not be possible for an empty list
public void Remove()
{
if(this.head == null)
{
return; // List is empty
}
if(this.curr == this.head)
{
this.head = this.head.getLink();
this.curr = this.head;
}
else
{
Node<Type> temp = this.head;
while(temp.getLink() != this.curr)
{
temp = temp.getLink();
}
temp.setLink(this.curr.getLink());
if(this.curr == this.tail)
{
this.tail = temp;
}
this.curr = temp;
}
this.num_items--;
if (this.num_items == 0) {
this.head = this.tail = this.curr = null; // List is empty
}
}
// replaces the value of the current element with the specified value
// this should not be possible for an empty list
public void Replace(Type data)
{
if(this.head != null)
{
this.curr.setData(data);
}
}
// returns if the list is empty
public boolean IsEmpty()
{
if(this.head == null)
{
return true;
}
else
{
return false;
}
}
// returns if the list is full
public boolean IsFull()
{
if(this.num_items == MAX_SIZE)
{
return true;
}
else
{
return false;
}
}
// returns if two lists are equal (by value)
public boolean Equals(List<Type> l)
{
if(this.num_items != l.num_items)
{
return false;
}
Node<Type> n = this.head;
Node<Type> m = l.head;
while(n != null)
{
if(!n.getData().equals(m.getData())) // Use .equals() instead of !=
{
return false;
}
n = n.getLink();
m = m.getLink();
}
return true;
}
// returns the concatenation of two lists
// l should not be modified
// l should be concatenated to the end of *this
// the returned list should not exceed MAX_SIZE elements
// the last element of the new list is the current
public List<Type> Add(List<Type> l)
{
List<Type> n = new List<Type>(this);
Node<Type> m = l.head;
while(m != null)
{
n.InsertAfter(m.getData());
m = m.getLink();
}
return n;
}
// returns a string representation of the entire list (e.g., 1 2 3 4 5)
// the string "NULL" should be returned for an empty list
public String toString()
{
if(this.head == null)
{
return "NULL";
}
else
{
Node<Type> n = this.head;
String s = "";
while(n != null)
{
s += n.getData() + " ";
n = n.getLink();
}
return s;
}
}
}