-
Notifications
You must be signed in to change notification settings - Fork 0
373 lines (327 loc) · 9.8 KB
/
[email protected]_transit_1_Transit.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import java.util.ArrayList;
/**
* This class contains methods which perform various operations on a layered linked
* list to simulate transit
*
* @author Ishaan Ivaturi
* @author Prince Rawal
*/
public class Transit {
/**
* Makes a layered linked list representing the given arrays of train stations, bus
* stops, and walking locations. Each layer begins with a location of 0, even though
* the arrays don't contain the value 0.
*
* @param trainStations Int array listing all the train stations
* @param busStops Int array listing all the bus stops
* @param locations Int array listing all the walking locations (always increments by 1)
* @return The zero node in the train layer of the final layered linked list
*/
//Pseudocode:
//make three new ArrayLists of TNodes for trains, busstops, an
public static TNode makeList(int[] trainStations, int[] busStops, int[] locations) {
//initializes arrayslists for nodes
ArrayList<TNode> bus = new ArrayList<TNode>();
bus.add(new TNode(0));
ArrayList<TNode> train = new ArrayList<TNode>();
train.add(new TNode(0));
ArrayList<TNode> walk = new ArrayList<TNode>();
//connect across
for (int i = 0; i <= locations.length; i++){
walk.add (new TNode(i, null, null));
}
for (int i = 0; i < busStops.length; i++){
bus.add (new TNode(busStops[i], null, null));
}
for (int i = 0; i < trainStations.length; i++)
{
train.add (new TNode(trainStations[i], null, null));
}
//connect down
for (int i = 0; i < walk.size()-1; i++)
{
walk.get(i).next = walk.get(i+1);
}
for (int i = 0; i < busStops.length; i++)
{
bus.get(i).next = bus.get(i+1);
}
for (int i = 0; i < trainStations.length; i++)
{
train.get(i).next = train.get(i+1);
}
for (int i = 0; i <= trainStations.length; i++){ //connects train nodes down
TNode busPosition= pos(bus,train.get(i).location);
train.get(i).down = busPosition;
}
for(int i = 0; i <= busStops.length; i ++){ //connects bus nodes down
bus.get(i).down = walk.get(bus.get(i).location);
}
return train.get(0);
}
private static TNode pos(ArrayList<TNode> buss, int position)
{
for (int i = 0; i < buss.size(); i ++)
{
if (buss.get(i).location == position)
{
return buss.get(i);
}
}
return null;
}
/**
* Modifies the given layered list to remove the given train station but NOT its associated
* bus stop or walking location. Do nothing if the train station doesn't exist
*
* @param trainZero The zero node in the train layer of the given layered list
* @param station The location of the train station to remove
*/
public static void removeTrainStation(TNode trainZero, int station)
{
// WRITE YOUR CODE HERE
TNode temp = trainZero;
ArrayList<TNode> train = new ArrayList<TNode>();
while (temp != null)
{
train.add(temp);
temp = temp.next;
}
while (trainZero !=null)
{
if (trainZero.next != null && station == trainZero.next.location )
{
trainZero.next = trainZero.next.next;
}
else if (trainZero.next == null)
{
trainZero = null;
}
if (trainZero!=null)
{
trainZero=trainZero.next;
}
}
}
/**
* Modifies the given layered list to add a new bus stop at the specified location. Do nothing
* if there is no corresponding walking location.
*
* @param trainZero The zero node in the train layer of the given layered list
* @param busStop The location of the bus stop to add
*/
public static void addBusStop(TNode trainZero, int busStop) {
// WRITE YOUR CODE HERE
TNode bus = trainZero.down;
TNode walk = trainZero.down.down;
//arraylist created
ArrayList<TNode> buss = new ArrayList<TNode>();
ArrayList<TNode> walkk = new ArrayList<TNode>();
while(bus!= null)
{
buss.add(bus);
bus=bus.next;
}
while(walk!= null)
{
walkk.add(walk);
walk=walk.next;
}
for (int i = 0; i<buss.size(); i++)
{
if (buss.get(i).location == busStop)
{
return;
}
}
for(int i=0; i< buss.size(); i++)
{
if (buss.get(i).next==null)
{
for(int j = 0; j<walkk.size(); j++){
if(walkk.get(j).location == busStop)
{
buss.get(i).next = new TNode(busStop, buss.get(i).next, walkk.get(j));
}
}
}
if(buss.get(i).next.location > busStop)
{
TNode check = null;
for (TNode j=trainZero.down.down; j.next!=null; j=j.next)
{
if(j.location==busStop)
{
check = j;
}
}
buss.get(i).next = new TNode(busStop, buss.get(i).next, check);
return;
}
}
}
/**
* Determines the optimal path to get to a given destination in the walking layer, and
* collects all the nodes which are visited in this path into an arraylist.
*
* @param trainZero The zero node in the train layer of the given layered list
* @param destination An int representing the destination
* @return
*/
public static ArrayList<TNode> bestPath(TNode trainZero, int destination) {
// WRITE YOUR CODE HERE
ArrayList<TNode> route = new ArrayList<TNode>();
//train route add
while(trainZero.next!=null&&trainZero.next.location<=destination)
{
route.add(trainZero);
trainZero = trainZero.next;
}
route.add(trainZero);
trainZero = trainZero.down;
route.add(trainZero);
//bus route add
while(trainZero.next!=null && trainZero.next.location<=destination)
{
trainZero = trainZero.next;
route.add(trainZero);
}
trainZero = trainZero.down;
//walk add
while(trainZero!=null &&trainZero.location<=destination)
{
route.add(trainZero);
trainZero = trainZero.next;
}
return route;
}
/**
* Returns a deep copy of the given layered list, which contains exactly the same
* locations and connections, but every node is a NEW node.
*
* @param trainZero The zero node in the train layer of the given layered list
* @return
*/
public static TNode duplicate(TNode trainZero) {
// WRITE YOUR CODE HERE
TNode train = new TNode(trainZero.location, trainZero.next, trainZero.down);
TNode bus = new TNode(trainZero.down.location, trainZero.down.next, trainZero.down.down);
TNode walk = (trainZero.down.down);
ArrayList<TNode> walkk = new ArrayList<TNode>();
ArrayList<TNode> buss = new ArrayList<TNode>();
ArrayList<TNode> trainn = new ArrayList<TNode>();
while (walk != null)
{
walkk.add(new TNode(walk.location, walk.next, null));
walk = walk.next;
}
while (bus != null)
{
buss.add(new TNode(bus.location, bus.next, bus.down));
bus = bus.next;
}
while (train != null)
{
trainn.add(new TNode(train.location, train.next, train.down));
train = train.
next;
}
ArrayList<TNode> dwalk = new ArrayList<TNode>();
ArrayList<TNode> dbus = new ArrayList<TNode>();
ArrayList<TNode> dtrain = new ArrayList<TNode>();
for (int i = 0; i <= walkk.size()-1; i ++)
{
dwalk.add (new TNode(i, null, null));
}
for (int i = 0; i < dwalk.size()-1; i++)
{
dwalk.get(i).next = dwalk.get(i+1);
}
for (int i = 0; i < buss.size(); i ++){
dbus.add (new TNode(buss.get(i).location, null, null));
}
for (int i = 0; i < dbus.size()-1; i++){
dbus.get(i).next = dbus.get(i+1);
}
for (int i = 0; i < dbus.size(); i++){
dbus.get(i).down = dbus.get(dbus.get(i).location);
}
for (int i = 0; i < trainn.size(); i ++){
dtrain.add (new TNode(trainn.get(i).location, null, null));
}
for (int i = 0; i < dtrain.size()-1; i++){
dtrain.get(i).next = dtrain.get(i+1);
}
for (int i = 0; i < dtrain.size(); i++){
int temp = buuu(dbus, dtrain.get(i).location);
dtrain.get(i).down = dbus.get(temp);
}
return dtrain.get(0);
}
private static int buuu(ArrayList<TNode> bus, int position)
{
for (int i = 0; i < bus.size(); i ++)
{
if (bus.get(i).location == position)
{
return i;
}
}
return 0;
}
/**
* Modifies the given layered list to add a scooter layer in between the bus and
* walking layer.
*
* @param trainZero The zero node in the train layer of the given layered list
* @param scooterStops An int array representing where the scooter stops are located
*/
public static void addScooter(TNode trainZero, int[] scooterStops)
{
TNode bus = trainZero.down;
TNode walk = bus.down;
TNode first=new TNode(0);
bus.down=first;
first.down=walk;
ArrayList<TNode> walkk = new ArrayList<TNode>();
ArrayList<TNode> buss = new ArrayList<TNode>();
while(walk != null)
{
walkk.add(walk);
walk= walk.next;
}
while(bus!= null)
{
buss.add(bus);
bus= bus.next;
}
ArrayList<TNode> sclist = new ArrayList<TNode>();
sclist.add(first);
for (int i = 0; i < scooterStops.length; i++)
{
sclist.add(new TNode (scooterStops[i]));
}
for (int i = 0; i <= scooterStops.length; i++)
{
sclist.get(i).down = walkk.get(sclist.get(i).location);
}
for (int i = 0; i < scooterStops.length; i++)
{
sclist.get(i).next = sclist.get(i+1);
}
for (int i = 0; i < buss.size(); i++){ //connects bus nodes down
TNode scooterPosition = scooterhelper(sclist, buss.get(i).location);
buss.get(i).down = scooterPosition;
}
}
private static TNode scooterhelper(ArrayList<TNode> scooter, int position){
for (int i = 0; i < scooter.size(); i ++)
{
if (scooter.get(i).location == position)
{
return scooter.get(i);
}
}
return null;
}
}