Skip to content

Instantly share code, notes, and snippets.

@ericwindmill
Created May 10, 2020 16:43
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/a3e3c3b014b1286e5ee51ad37581cd1f to your computer and use it in GitHub Desktop.
Save ericwindmill/a3e3c3b014b1286e5ee51ad37581cd1f to your computer and use it in GitHub Desktop.
fbe_dart_iterables_remove_elements_stack
Peek should show the element that was added least recently, but not remove it
/// solution
class Stack<T> {
List<T> _internal = <T>[];
T peek() {
// implement peek
}
T pop() {
// implement pop
}
void push(T value) {
// implement push
}
}
/// solution
class Stack<T> {
List<T> _internal = <T>[];
T peek() => _internal.last;
T pop() => _internal.removeLast();
void push(T value) => _internal.add(value);
}
void main() {
final stack = Stack<int>();
stack.push(1);
stack.push(2);
if (stack.peek() != 2) {
_result(false, ["peek did not return the top element in the stack"]);
}
if (stack._internal.length != 2) {
_result(false, ["expected stack of length 2 after adding two elements and peeking"]);
}
final popped = stack.pop();
if (popped != 2) {
_result(false, ["expected the last element added to the stack when `push` 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