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.
Heads up... You’re accessing parts of this content for free, with some sections shown as tbvazvwyg text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
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:
classDequeLinkedList<E> implementsDeque<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.
@overrideboolget 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;
}
}
Heads up... You’re accessing parts of this content for free, with some sections shown as vjmoxtfar text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
You’re accessing parts of this content for free, with some sections shown as cqdihdzob text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.