如何以相反的顺序遍历C#LinkedList
如何使用C# LinkedList
执行以下C ++代码段的等效操作?
std::list::reverse_iterator itr(it); for(; itr != MyList.rend(); ++itr)
作为一次性,像:
var el = list.Last; while (el != null) { // use el.Value el = el.Previous; }
如果你经常这样做,可能会有一个类似的迭代器块来产生所有的值:
public static IEnumerable Reverse (this LinkedList list) { var el = list.Last; while (el != null) { yield return el.Value; el = el.Previous; } }
然后:
foreach(var val in list.Reverse()) { // use val }