LinkedList<Node>
does not contain a definition for 'begin' and no extension method 'begin' accepting a first argument of type 'LinkedList<Node>
' could be found (are you missing a using directive or an assembly reference?)LinkedList
不包含'begin'的定义,并且没有扩展方法'begin'可以找到接受类型'LinkedList '的第一个参数(你是否缺少using指令或汇编引用?)
I rewrote this from c++ to C#, what function do I use instead of "begin" since there's no function for it in LinkedList?
我把它从c ++重写为C#,我使用什么函数而不是“begin”,因为LinkedList中没有它的功能?
Code
static int main
{
LinkedList<LinkedList<Node>> l = new LinkedList<LinkedList<Node>>();
LinkedList<LinkedList<Node>>.Enumerator itr;
LinkedList<Node>.Enumerator itr1;
Binary_Tree bt = new Binary_Tree();
bt.insert(3);
bt.insert(2);
bt.insert(4);
bt.insert(1);
bt.insert(5);
l = new LinkedList<LinkedList<Node>>(bt.level());
itr = l.GetEnumerator();
while (itr.MoveNext())
{
itr1 = (itr.Current).begin();
while (itr1.MoveNext())
{
Console.Write((itr1.Current).data);
Console.Write(" ");
}
Console.Write("\n");
}
return 0;
}
1 个解决方案
#1
There's a variety of ways that you can approach this with or without iterators, and the comments so far are also useful and on target.
有或没有迭代器,你可以通过多种方式处理这个问题,到目前为止,这些评论在目标上也是有用的。
If you are interested in something that traverses the nodes explicitly (rather than, say, foreach
, which certainly is a nice and simple approach), but does not need an iterator, you can use the First
property of the LinkedList. This uses 'LinkedListNode' (which is returned by First
, a Node wrapper on the Node data item that supplies a Next
and a Previous
, etc.
如果你对那些明确遍历节点的东西感兴趣(而不是,比如foreach,这当然是一个很好的简单方法),但是不需要迭代器,你可以使用LinkedList的First属性。这使用'LinkedListNode'(由First返回,节点数据项上的节点包装器提供Next和Previous等。
var outerNode = l.First;
while (outerNode != null) {
var innerNode = outerNode.Value.First;
while (innerNode != null) {
Console.Write(innerNode.Value.data);
Console.Write(" ");
innerNode = innerNode.Next;
}
Console.Write("\n");
outerNode = outerNode.Next;
}
#1
There's a variety of ways that you can approach this with or without iterators, and the comments so far are also useful and on target.
有或没有迭代器,你可以通过多种方式处理这个问题,到目前为止,这些评论在目标上也是有用的。
If you are interested in something that traverses the nodes explicitly (rather than, say, foreach
, which certainly is a nice and simple approach), but does not need an iterator, you can use the First
property of the LinkedList. This uses 'LinkedListNode' (which is returned by First
, a Node wrapper on the Node data item that supplies a Next
and a Previous
, etc.
如果你对那些明确遍历节点的东西感兴趣(而不是,比如foreach,这当然是一个很好的简单方法),但是不需要迭代器,你可以使用LinkedList的First属性。这使用'LinkedListNode'(由First返回,节点数据项上的节点包装器提供Next和Previous等。
var outerNode = l.First;
while (outerNode != null) {
var innerNode = outerNode.Value.First;
while (innerNode != null) {
Console.Write(innerNode.Value.data);
Console.Write(" ");
innerNode = innerNode.Next;
}
Console.Write("\n");
outerNode = outerNode.Next;
}