Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a stack with push and pop in Dart

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();
like image 872
Suragch Avatar asked Sep 25 '20 08:09

Suragch


People also ask

How do you make a dart stack?

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.

Does a stack use push and pop?

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.

How do you define a stack in darts?

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.


2 Answers

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.

enter image description here

Implementation

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:

  • To push means to add a value to the top of the stack. This is implemented here with _list.add, which is a fast O(1) operation.
  • To pop means to remove a value from the top of the stack. This is implemented here with _list.removeLast, which is a fast O(1) operation.
  • To peek means to get the value of the top element in the stack without actually removing it. This is implemented here with _list.last, which is a fast O(1) operation.

Nullable implementation

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;

Usage

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
like image 63
Suragch Avatar answered Sep 23 '22 00:09

Suragch


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;

}
like image 44
alireza easazade Avatar answered Sep 21 '22 00:09

alireza easazade