-
Notifications
You must be signed in to change notification settings - Fork 0
/
BusStopList.java
185 lines (159 loc) · 5.81 KB
/
BusStopList.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
package MMN15;
/**
* This class bild and work with linked list BusStopList.
*
* @author Aleksey Yurovskiy
* @ID 327153904
*/
public class BusStopList
{
BusArrivalNode _head;
/** This is constractor.
*
*/
public BusStopList(){
_head = null;
}
/** This method add new bus arrival into bus list if it sucsesfull return true.
* The method add arrival only if list hasn't similar arrival. Time complexity O(1)
* Space Complexity O(1)
* @param int lineNumber - bus number;
* int noOfPassengers - number of passengers;
* Time1 arrivalTime - arrival time bus.
* @return true if add sucsesfull.
*/
public boolean add(int lineNumber,int noOfPassengers, Time1 arrivalTime){
BusArrival busArr = new BusArrival(lineNumber, noOfPassengers, arrivalTime);
//check if we not add similar BusArrival.//
if( equalsInList(busArr) )
return false;// not added node because busArr in list.
if(_head == null)
_head = new BusArrivalNode(busArr);
else{
BusArrivalNode busArrivalNode = new BusArrivalNode(busArr, _head);
_head = busArrivalNode;
}
return true;
}
private boolean equalsInList(BusArrival b){
//This method return true if it find BusArrival b in BusStopList.
//Time complexity O(n). Space Complexity O(1)
BusArrivalNode pointer = _head;
while (pointer != null && !pointer.getBusArr().equals(b)){
pointer = pointer.getNext();
}
if(pointer == null){
return false;// if not find Bus Arrival return false.
}
return true; // if find Bus Arrival return true.
}
/** This method remove all nodes with number's line "lineNum" from BusStopList.
* Time complexity O(n). Space Complexity O(1)
* @param int lineNum - removed node with lineNum.
*/
public void removeAllLine(int lineNum){
BusArrivalNode pointer = _head;
BusArrivalNode prev = null;
while(pointer != null){
while (pointer!= null && pointer.getBusArr().getLineNum() != lineNum){
prev = pointer;
pointer = pointer.getNext();
}
if(prev == null)
_head = _head.getNext();
else if(pointer != null)
prev.setNext(pointer.getNext() );
if(pointer!= null)
pointer = pointer.getNext();
}
}
/** This method return string representation of bus stop list. Time complexity O(n)
* Space Complexity O(1)
* @return String - str.
*/
public String toString(){
BusArrivalNode pointer = _head;
String str = "";
while( pointer != null){
str += pointer.getBusArr().toString() + "\n";
pointer = pointer.getNext();
}
return str;
}
/**
* This method return most popular line in list. Time complexity O(n)
* Space Complexity O(n)
* @return int - nuber most popular line.
*/
public int getPopularLine()
{
final int MAXIMUM_LINES = 99;
int[] arrLine = new int [MAXIMUM_LINES + 1];// +1 because line number 99 included.
BusArrivalNode pointer = _head;
while(pointer != null){
arrLine[pointer.getBusArr().getLineNum()] += + 1;
pointer = pointer.getNext();
}
return getPopularLine(arrLine);
}
private int getPopularLine(int[] arrLine){
/* This method search biggest digit in array arrLine and return it.
Time complexity O(n). Space Complexity O(1) */
final int MAXIMUM_LINES = 99;
int curIndexLine;
int popularLine = 0;
//Every cell of array this is bus line.
for (curIndexLine = 0; curIndexLine < MAXIMUM_LINES; curIndexLine++){
if(arrLine[curIndexLine] < arrLine[curIndexLine + 1]){
popularLine = curIndexLine + 1;
}
}
return popularLine;
}
/** This method return average waiting time for arrival bus any line. Time complexity O(n)
* Space Complexity O(1)
* @return long - seconds.
*/
public long getAverageTime(){
long elapsedTime = 0;
int count = 0;//need for founding average.
final int SEC_IN_MINUTE = 60;
BusArrivalNode pointer = _head.getNext();
BusArrivalNode prev = _head;
while(pointer!= null){
elapsedTime += prev.getBusArr().elapsedTime(pointer.getBusArr());
prev = pointer;
pointer = pointer.getNext();
count++;
}
return elapsedTime/ count * SEC_IN_MINUTE;
}
/** This methos return total number of passengers in list. Time complexity O(n)
* Space Complexity O(1)
* @return int - number passengers in list.
*/
public int totalPassengers(){
int totalPassengers = 0;
BusArrivalNode p = _head;
while(p != null){
totalPassengers += p.getBusArr().getNoOfPass();
p = p.getNext();
}
return totalPassengers;
}
/** This method return bus arrival with max number passengers in list. Time complexity O(n)
* Space Complexity O(1)
* @return BusArrival - max number passengers.
*/
public BusArrival maxPassengers(){
int pass = 0;
BusArrivalNode maxPassengersResult = _head;
BusArrivalNode pointer = _head.getNext();
while(pointer != null){
if(pointer.getBusArr().getNoOfPass() > maxPassengersResult.getBusArr().getNoOfPass())
maxPassengersResult = pointer;
pointer = pointer.getNext();
}
return new BusArrival(maxPassengersResult.getBusArr());
}
}