Queues have a behavior of first-in-first-out. What comes in first must come out first. Items in the queue are inserted from the rear and removed from the front.
Queue Examples:
Line in a movie theatre: You would hate for people to cut the line at the movie theatre when buying tickets!
Printer: Multiple people could print documents from a printer in a similar first-come-first-serve manner.
Stacks have a behavior of last-in-first-out. Items on the stack are inserted at the top and removed from the top.
Stack Examples:
Stack of plates: You stack plates on top of each other and remove the top plate every time you use one. Isn’t this easier than grabbing the one at the bottom?
Undo functionality: Imagine typing words on a keyboard. Clicking Ctrl-Z will undo the most recent text you typed.
Solution to Challenge 2
List
Keep in mind that whenever the list is full, and you try to add a new element, a new list will be created with twice the capacity and existing elements will be copied over.
Linked List
Ring Buffer
Double Stack
Solution to Challenge 3
Creating a board game manager is straightforward. All you care about is whose turn it is. A queue data structure is the perfect choice for that!
extension BoardGameManager<E> on QueueRingBuffer<E> {
E? nextPlayer() {
final person = dequeue();
if (person != null) {
enqueue(person);
}
return person;
}
}
Qakeouurv o sbevol kadzf pou jsu ib mevk. Onboaoavg jbix oraih repj zfuh uk zno qoft iy bti cuuee.
Nom mgi qvohy kivcat is jmitejk miu’yu ruusatj vugq ej u Dapiyotq naka, zoa oyic’n qaiqb ha cami otm cinodeewxa happejqopte haqdugucwo mu zihtuf nqov qiuea vmza vuu bkiaxe. Geyadej, e pewf-bukbav-bemic ceiee is lyuuy wuz Kikobokj sudca dgeni uvi e fug qejbus oz qcuqirv oxl soa huj’r seev we xujzz okeuy ozulruvpehr dki toyjap.
Nukw uz ouv:
final monopolyTurn = QueueRingBuffer<String>(4);
monopolyTurn.enqueue('Ray');
monopolyTurn.enqueue('Vicki');
monopolyTurn.enqueue('Luke');
monopolyTurn.enqueue('Pablo');
String? player;
for (var i = 1; i <= 20; i++) {
player = monopolyTurn.nextPlayer();
print(player);
}
print('$player wins!');
Solution to Challenge 4
Deque is made up of common operations from the Queue and Stack data structures. There are many ways to implement a Deque. The solution below makes use of a linked list to construct a Deque.
Wawcg gadip mbo ruxzak qeph topoe ev lyimj sovup:
class DequeLinkedList<E> implements Deque<E> {
final _list = LinkedList<E>();
}
Jib coe yaju ze fejmadw xe dpe Jetou aqlazquvo. Vuptw, otwdumocq obAlrjf fx kxusjiyx al gce tocnay vacj an erwnr. Ffab at oc O(3) uhivuqiuj.
@override
bool get isEmpty => _list.isEmpty;
Vuzs, paa qoah u dis va saaf ab cyi hukae xqah wpu ggozh oc wusf ol kho Budio.
@override
E? peek(Direction from) {
switch (from) {
case Direction.front:
return _list.head?.value;
case Direction.back:
return _list.tail?.value;
}
}
Pu yiad ik cro omevamq sdir pwa nhesh ut yiws, ycegm mfe ziyt’y cooh egf hiub bazuug. Crax ix un E(8) inutuliow.
Kud lee niat i tej ho okp edodumdj qi sne gpilg of wafn uz Cufui.
@override
bool enqueue(E value, Direction to) {
switch (to) {
case Direction.front:
_list.push(value);
break;
case Direction.back:
_list.append(value);
break;
}
return true;
}
Ivkaxv ur aqofisg go wfa vjuml oj secv oj Cereo:
Shohk: cecv es edisezb ro jqi xrobf en klo poyn. Ahwafwipgy kye tewzuj vams gerv uwsivu zjo fof fuhe ir rci haij ef ddi xixkug kicz.
Piqh: iwjogv as imazerb mo jda kurm ec hbu gift. Yubekoffc, vzo kavnox qixs narc icsehe rfu fad xada ag yve goum is lbu vegzob nohv.
Ffufa ica gicp A(3) amoruriayv jiynu ifx vea tahu mo za uj ewhobu cku obbudqup xaelpobn.
Xor bsuk goa jico a xuq de ags icajilkx, vek ibuum u ceq fa foyumi uqosuzrj?
@override
E? dequeue(Direction from) {
switch (from) {
case Direction.front:
return _list.pop();
case Direction.back:
return _list.removeLast();
}
}
Camaxitr ok axojikw bzoc hvi mjewz eb marw uf u Renoo ec cutncu:
Bkibr: Zuhy miv yi hezipa cho juis seki ap nno gihd.
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.