Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to define a Stack in dartPad?

https://dartpad.dev/?

import 'package:starter/stack.dart';

void main() {
print (calculation("(2+2*6"));
}

 calculation(expression){
   var tokens = expression.split(" ");
   Stack values = Stack();
   Stack ops = Stack();
}

note that if I remove the import (first line) I get this error message "The function ‘Stack’ isn’t defined."
and when I add the import the error message is "Unsupported import(s): (package:starter/stack.dart)".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Stack is not built in the dart. But we can make a generic class with its methods(get, push, pop) implementation using list.

class CustomStack<T> {
  final _list = <T>[];

  void push(T value) => _list.add(value);

  T pop() => _list.removeLast();

  T get top => _list.last;

  bool get isEmpty => _list.isEmpty;
  bool get isNotEmpty => _list.isNotEmpty;

  @override
  String toString() => _list.toString();
}

Just put the above class somewhere in the project lib directory.
And use it like.

CustomStack<String> plates = CustomStack();
//Add plates into the stack
  plates.push("Plate1");
  plates.push("Plate2");
  plates.push("Plate3");
// delete the top plate
  plates.pop();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading