Skip to content

Instantly share code, notes, and snippets.

@ericwindmill
Created May 10, 2020 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericwindmill/befbabc698e2ddff75a063efebe68e23 to your computer and use it in GitHub Desktop.
Save ericwindmill/befbabc698e2ddff75a063efebe68e23 to your computer and use it in GitHub Desktop.
fbe_dart_iterables_add
Peek should show the element that was added least recently, but not remove it.
class Queue<T> {
/// internal represents the list that Queue wraps. You do not need to edit this line.
List<T> _internal = <T>[];
T peek() {
// implement peek
// which shows the next element to be removed from the list
}
T dequeue() {
// implement dequeue
// which removes the proper element from the list
}
void enqueue(T value) {
// implement enqueue
// which adds an element to the proper place in the list.
}
}
class Queue<T> {
List<T> _internal = <T>[];
T peek() => _internal.first;
T dequeue() => _internal.removeAt(0);
void enqueue(T value) => _internal.add(value);
}
void main() {
final queue = Queue<int>();
queue.enqueue(1);
if (queue.peek() != 1) {
_result(false, ["peek did not return the first element"]);
}
if (queue._internal.length != 1) {
_result(false, ["expected queue of length 1 after adding one element and peeking"]);
}
final popped = queue.dequeue();
if (popped != 1) {
_result(false, ["expected the first element added to the queue when `dequeue` is called."]);
}
_result(true, ["Good work, fam"]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment