【问题标题】:Implementing Iterable class on a Linked List in Dartlang?在 Dartlang 的链表上实现 Iterable 类?
【发布时间】:2019-06-23 04:59:00
【问题描述】:

我知道 dart:collection 库有一个很棒的链表实现。然而,我正在尝试自己实现一个链接列表作为 MOOC 的一部分。

这是我非常简单的链表实现

import './node.dart';

class LinkedList {
  // root node of the list
  Node root = null;
  // the number of items on the list
  int _count = 0;

  // returns true if root is not set
  bool isEmpty(){
    return this.root == null;
  }

  // getter for the _count variable;
  get count{
    return this._count;
  }

  // push method to push a new item to the end of the list
  push(String content) {
    Node item = new Node(content);
    if (this.root == null) {
      this._count += 1;
      this.root = item;
    } else {
      Node iterator = root;
      while (iterator.next != null) {
        iterator = iterator.next;
      }
      this._count += 1;
      iterator.next = item;
    }
    return item;
  }
}

我想让它实现 Iterable Class 属性和方法,例如 foreach 和 length 。我阅读了 Iterable 和 IterableMixin 类的文档,但我仍在努力理解如何将它们与我的 LinkedList 类一起使用,因为这些文档仅提供了将 Map 用作 Iterable 的示例。

【问题讨论】:

    标签: linked-list dart iterable


    【解决方案1】:

    扩展IterableBase docs,你应该很高兴!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 2019-10-27
      • 2014-12-19
      • 1970-01-01
      • 2015-03-08
      • 1970-01-01
      相关资源
      最近更新 更多