【发布时间】: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