Chapters

Hide chapters

Data Structures & Algorithms in Dart

Second Edition · Flutter · Dart 3.0 · VS Code 1.78

Section VI: Challenge Solutions

Section 6: 21 chapters
Show chapters Hide chapters

6. Chapter 6 Solutions
Written by Jonathan Sande

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

Solution to Challenge 1

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:

  1. Line in a movie theatre: You would hate for people to cut the line at the movie theatre when buying tickets!
  2. 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:

  1. 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?
  2. 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.

D M I O H Q D A I V W avfuiao ('R') M V E A L T A uskoeui ('I') V E I L X E pebeauu () J A O N Z E P irpiioi ('F') O A F R O B xiqueuo () U V P U B lamouuu () E X P U S R eztaaoi ('M')

Linked List

S P E E D S P E E D D enqueue ('D') P E E D A D R enqueue ('R') E E D A D R dequeue () E D A D R dequeue () enqueue ('T') E D A D R T S P E E D D A enqueue ('A') dequeue () P E E D D A

Ring Buffer

S P E E D r w S P E E D r w S P E E D r w S P E E D r w r w R P E E D r w R P E E D r w R P E E D r w R T E E D enqueue ('D') return false enqueue ('A') return false enqueue ('R') return true enqueue ('T') dequeue () dequeue () dequeue ()

Double Stack

Left Stack S Right Stack E P E S D D Left Stack Right Stack enqueue ('D') E P E D

Xujk Mnasq Z W A A R M O Pigvp Tdedl T A N P N I O Pawp Mvafx Vapqp Qqojt nogueae () iwneuio ('A')

Zajg Bcoyq T Vufzz Ztutc B E V S Y O I Gowl Qrosj Pacgp Sfukn joyaaae () ehlouau ('Q') P E W P O O

F I W A M O Pops Ppinc Malvk Xgelz seliuee () A S B A Q F Tikh Bxejh Xasyr Kcomp ikxaiui ('W')

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;
  }
}
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.

class DequeLinkedList<E> implements Deque<E> {
  final _list = LinkedList<E>();

}
@override
bool get isEmpty => _list.isEmpty;
@override
E? peek(Direction from) {
  switch (from) {
    case Direction.front:
      return _list.head?.value;
    case Direction.back:
      return _list.tail?.value;
  }
}
@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;
}
@override
E? dequeue(Direction from) {
  switch (from) {
    case Direction.front:
      return _list.pop();
    case Direction.back:
      return _list.removeLast();
  }
}
@override
String toString() => _list.toString();
final deque = DequeDoublyLinkedList<int>();
deque.enqueue(1, Direction.back);
deque.enqueue(2, Direction.back);
deque.enqueue(3, Direction.back);
deque.enqueue(4, Direction.back);

print(deque);

deque.enqueue(5, Direction.front);

print(deque);

deque.dequeue(Direction.back);
deque.dequeue(Direction.back);
deque.dequeue(Direction.back);
deque.dequeue(Direction.front);
deque.dequeue(Direction.front);
deque.dequeue(Direction.front);

print(deque);
1 -> 2 -> 3 -> 4
5 -> 1 -> 2 -> 3 -> 4
Empty list
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

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.

Unlock now