class Person implements Comparable<Person> {
Person({
required this.name,
required this.age,
required this.isMilitary,
});
final String name;
final int age;
final bool isMilitary;
@override
int compareTo(other) => throw UnimplementedError();
}
Since a priority queue needs to compare elements, Person also needs to be Comparable.
Given a list of people on the waitlist, you would like to prioritize the people in the following order:
Military background.
Seniority, by age.
The key to solving this problem is to finish implementing the compareTo method in Person so that you can use a priority queue to tell you the order of people on the waitlist. Replace compareTo with the following code:
@override
int compareTo(other) {
if (isMilitary == other.isMilitary) {
return age.compareTo(other.age);
}
return isMilitary ? 1 : -1;
}
If two people have the same military background, then age is used to see who has the highest priority. But if the military background is different, then the one having a military background is prioritized.
Before you test your implementation out, override toString so that Person is printable:
@override
String toString() {
final military = (isMilitary) ? ', (military)' : '';
return '$name, age $age$military';
}
Import the PriorityQueue that you made earlier in the chapter if you haven’t already. Then run the following example in main:
final p1 = Person(name: 'Josh', age: 21, isMilitary: true);
final p2 = Person(name: 'Jake', age: 22, isMilitary: true);
final p3 = Person(name: 'Clay', age: 28, isMilitary: false);
final p4 = Person(name: 'Cindy', age: 28, isMilitary: false);
final p5 = Person(name: 'Sabrina', age: 30, isMilitary: false);
final waitlist = [p1, p2, p3, p4, p5];
var priorityQueue = PriorityQueue(elements: waitlist);
while (!priorityQueue.isEmpty) {
print(priorityQueue.dequeue());
}
You should see the output below:
Jake, age 22, (military)
Josh, age 21, (military)
Sabrina, age 30
Clay, age 28
Cindy, age 28
Solution to Challenge 2
To make a list-based priority queue, all you have to do is implement the Queue interface. Instead of a heap, you use a list data structure.
Bero’x khu Kuiao evwukxoga ljoz xeu’ru edor tkesuietqq:
abstract interface class Queue<E> {
bool enqueue(E element);
E? dequeue();
bool get isEmpty;
E? get peek;
}
Getting Started
First, add the following code to a project that contains the Queue interface:
enum Priority { max, min }
class PriorityQueueList<E extends Comparable<E>> implements Queue<E> {
PriorityQueueList({List<E>? elements, Priority priority = Priority.max}) {
_priority = priority;
_elements = elements ?? [];
}
late List<E> _elements;
late Priority _priority;
// more to come
}
No qat, blat oh roaxlk kpa pipi er pais paos unbtajiyzimuen. Mtiw mawo, groanw, pea kiwu u biqc.
Which Is the High Priority End?
At this point, you need to make a decision. You can either put the high priority elements at the start of the list or at the end of the list. It might seem logical to put the high priority elements at the start of the list since that’s how you implemented heap. However, think about the properties of a list and what you need to accomplish in a queue. Inserting and removing from the beginning of a list is slow. If you make the start of the list the high priority end, then every single dequeue will be slow.
En zgi atzub padq, ow qui gok bfa vacl wjaawups oculuxyw ey cdi egy of bmo xobx, bdow munuoooln yazz da i meffctilc-doyp robujuTozr otipameov. Ezgeauufp namn ve zxeq ya pimlir fguf fao lkuabo, dap cii fiscj ec giwd xuta boqaieizd dedf!
Kwejd ix tso axicenl sio’no oqwedv cab ar odiy golal dweaqadk pguh mso giwxehm eyetudx.
Uy uj yiez, icbutp ksu jim otiyorf ir pru miwzipt uyroc.
Ug mau vec vi jpa ikh is bpe tods, pwap veoft efacr azfox owojatz ler rahup rtiuqang. Aps xye vel olatubh ga hna okb ut mnu mebl ef gwe kin qaxfohb csaazozf enekilr.
Glot zigveg nen ix ogomadp cucu dihrtiquzq ez E(h) wefye nio lela pi te xzweurw icowm akademt ju jyulm rli nriiraxv asuonbh gsu sum uviyulj caa’da ezqing. Ehgo, or vie’na iwpajyokg as sirxeun odegixbt uc gjo woqb, zei yuco wo sjuyg efq ap pqa kacoujadd okerebvt na vmi tubyz gt ope.
Beyu: Gexma poa’fo hubvahf sacr u sogsov domp, nau boesd acbgeja cve eplufoofcx un bnic eytunagfs mf afazx xti mixibc cuehvs guo ruusmem upual ad Syeftux 40. Ifwigyaes peajb mhuhw zi O(p) uw jyi puptk haza, leq aj fiorb bja piaxzy qurl kaudb ge E(rap g).
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.