Files
hello-algo/en/codes/dart/chapter_stack_and_queue/stack.dart
Yudong Jin 14c314c08c Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
2026-10-31 03:35:51 +08:00

36 lines
847 B
Dart

/**
* File: stack.dart
* Created Time: 2023-03-27
* Author: liuyuxin (gvenusleo@gmail.com)
*/
void main() {
/* Initialize the stack */
// Dart has no built-in stack class; a List can be used as a stack
final List<int> stack = [];
/* Push elements onto the stack */
stack.add(1);
stack.add(3);
stack.add(2);
stack.add(5);
stack.add(4);
print("Stack stack = $stack");
/* Access the top element */
final int peek = stack.last;
print("Top element peek = $peek");
/* Pop an element from the stack */
final int pop = stack.removeLast();
print("Popped element pop = $pop; after popping, stack = $stack");
/* Get the stack size */
final int size = stack.length;
print("Stack size = $size");
/* Check whether it is empty */
final bool isEmpty = stack.isEmpty;
print("Is the stack empty = $isEmpty");
}