I'd like to implement a stack data structure (not to be confused with the Flutter Stack widget) in Dart so that I can handle a stack of custom TextStyles for Flutter text rendering.
I know with stack you can push and pop values. This sounds similar to Queue, but I'm not sure of the difference.
This doesn't work:
final myStack = Queue<int>();
myStack.push(1);
final top = myStack.pop();
Open up the starter project for this chapter. In the root of the project add a folder named lib, and in that folder create a file named stack. dart. Note: If you are using DartPad rather than a full IDE, then just create your Stack class outside of the main function.
In computer science, a stack is an abstract data type that serves as a collection of elements, with two main operations: Push, which adds an element to the collection, and. Pop, which removes the most recently added element that was not yet removed.
Dart data structures: StackA stack is an abstract collection that stores data in an ordered sequence. There is only one point of entry and exit in a stack. A stack uses the model of last in, first out (LIFO) — the last item that goes into the stack is also the first item that goes out of the stack.
A stack is a first-in-last-out (FILO) data structure. If you make a stack of books, the first book you put down will be covered by any other books that you stack on top of it. And you can't get that book back until you remove all of the other books on top of it.
You can implement a stack a number of different ways. The original version of this answer (see edit history) used a Queue
as the underlying data structure. However, the default Dart Queue itself uses a list, so it seems like a List
is the more straightforward approach. Here is how I would implement a stack now:
class Stack<E> {
final _list = <E>[];
void push(E value) => _list.add(value);
E pop() => _list.removeLast();
E get peek => _list.last;
bool get isEmpty => _list.isEmpty;
bool get isNotEmpty => _list.isNotEmpty;
@override
String toString() => _list.toString();
}
Notes:
_list.add
, which is a fast O(1) operation._list.removeLast
, which is a fast O(1) operation._list.last
, which is a fast O(1) operation.When using the above implementation of stack, you would normally check isNotEmpty
before trying to pop
or peek
because doing so on an empty stack would cause the underlying List
to throw an error. However, if you prefer to check for null
instead, you can make pop
and peek
nullable:
E? pop() => (isEmpty) ? null : _list.removeLast();
E? get peek => (isEmpty) ? null : _list.last;
You can use your Stack
like so:
void main() {
final myStack = Stack<String>();
myStack.push('Green Eggs and Ham');
myStack.push('War and Peace');
myStack.push('Moby Dick');
while (myStack.isNotEmpty) {
print(myStack.pop());
}
}
This is the output:
Moby Dick
War and Peace
Green Eggs and Ham
here is the class I use
import 'dart:collection';
class Stack<T> {
final _stack = Queue<T>();
int get length => _stack.length;
bool canPop() => _stack.isNotEmpty;
void clearStack(){
while(_stack.isNotEmpty){
_stack.removeLast();
}
}
void push(T element) {
_stack.addLast(element);
}
T pop() {
T lastElement = _stack.last;
_stack.removeLast();
return lastElement;
}
T peak() => _stack.last;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With