在 Dartlang 的链表上实现 Iterable class?
Implementing Iterable class on a Linked List in Dartlang?
我知道 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 class 的文档,但我仍在努力理解如何将它们与我的 LinkedList class 一起使用,因为文档仅给出了将 Map 用作 Iterable 的示例。
扩展 IterableBase
docs,你应该可以开始了!
我知道 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 class 的文档,但我仍在努力理解如何将它们与我的 LinkedList class 一起使用,因为文档仅给出了将 Map 用作 Iterable 的示例。
扩展 IterableBase
docs,你应该可以开始了!