Java Reference
In-Depth Information
12 priorityQueue.enqueue(patient3);
13 priorityQueue.enqueue(patient4);
14
15 while (priorityQueue.getSize() > 0 )
16 System.out.print(priorityQueue.dequeue() + " " );
17 }
18
19
remove from queue
static class Patient implements Comparable<Patient> {
inner class Patient
20
private String name;
21
private int priority;
22
23
public Patient(String name, int priority) {
24
this .name = name;
25
this .priority = priority;
26 }
27
28 @Override
29
public String toString() {
30
return name + "(priority:" + priority + ")" ;
31 }
32
33 @Override
34
public int compareTo(Patient patient) {
compareTo
35
return this .priority - patient.priority;
36 }
37 }
38 }
Cindy(priority:7) Tim(priority:5) John(priority:2) Jim(priority:1)
24.21
What is a priority queue?
Check
24.22
What are the time complexity of the enqueue , dequeue , and getSize methods in
MyProrityQueue ?
Point
24.23
Which of the following statements are wrong?
1 MyPriorityQueue<Object> q1 = new MyPriorityQueue<>();
2 MyPriorityQueue<Number> q2 = new MyPriorityQueue<>();
3 MyPriorityQueue<Integer> q3 = new MyPriorityQueue<>();
4 MyPriorityQueue<Date> q4 = new MyPriorityQueue<>();
5 MyPriorityQueue<String> q5 = new MyPriorityQueue<>();
C HAPTER S UMMARY
1.
You learned how to implement array lists, linked lists, stacks, and queues.
2. To define a data structure is essentially to define a class. The class for a data structure
should use data fields to store data and provide methods to support operations such as
insertion and deletion.
3. To create a data structure is to create an instance from the class. You can then apply the
methods on the instance to manipulate the data structure, such as inserting an element
into the data structure or deleting an element from the data structure.
4.
You learned how to implement a priority queue using a heap.
 
 
Search WWH ::




Custom Search