如何使用c# LinkedList
实现以下c++代码段的等效功能?
std::list<MyClass*>::reverse_iterator itr(it);
for(; itr != MyList.rend(); ++itr)
作为1-off,例如:
var el = list.Last;
while (el != null) {
// use el.Value
el = el.Previous;
}
如果你经常这样做,也许一个类似的迭代器块来产生所有的值:
public static IEnumerable<T> Reverse<T>(this LinkedList<T> list) {
var el = list.Last;
while (el != null) {
yield return el.Value;
el = el.Previous;
}
}
:
foreach(var val in list.Reverse()) {
// use val
}