-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPQClient.java
More file actions
32 lines (29 loc) · 1.1 KB
/
PQClient.java
File metadata and controls
32 lines (29 loc) · 1.1 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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.Transaction;
public class PQClient {
public static void main(String[] args) {
int m = Integer.parseInt(args[0]);
MaxPQTest<Transaction> pq = new MaxPQTest<Transaction>(m + 1);
while (StdIn.hasNextLine()) {
String line = StdIn.readLine();
Transaction transaction = new Transaction(line);
pq.insert(transaction);
if (pq.size() > m) {
pq.delMax();
}
// low M entries are on the PQ
Stack<Transaction> st = new Stack<Transaction>();
for (Transaction tr : pq)
st.push(tr);
for (Transaction tr : st)
StdOut.println(tr);
}
}
}