Created
May 10, 2020 16:09
-
-
Save ericwindmill/befbabc698e2ddff75a063efebe68e23 to your computer and use it in GitHub Desktop.
fbe_dart_iterables_add
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Peek should show the element that was added least recently, but not remove it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Queue<T> { | |
List<T> _internal = <T>[]; | |
T peek() => _internal.first; | |
T dequeue() => _internal.removeAt(0); | |
void enqueue(T value) => _internal.add(value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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