【发布时间】:2009-07-29 21:48:18
【问题描述】:
我正在尝试移动列表中的项目,但是当我与最后一个选项进行比较时,我在移动移动链接列表中的项目之前退出了。有没有办法在节点被放在最后并且无法循环移动项目之前做到这一点?
LinkedList<BD> list = new LinkedList<BD>(b[arg].Values);
LinkedListNode<BD> node, terminator, next = null;
List<LinkedListNode<BD>> move = new List<LinkedListNode<BD>>();
terminator = list.First;
node = next = list.Last;
while (next != null && next != terminator)
{
node = next;
next = next.Previous;
if (IDs.Contains(node.Value.Id))
{
move.Add(node);
list.Remove(node);
}
else
{
foreach (var item in move)
{
list.AddBefore(node, item);
node = node.Previous;
}
move.Clear();
}
}
【问题讨论】:
-
请编辑您的问题,选择代码部分,然后按编辑器上方的小“代码”按钮。这使得代码更容易阅读。
-
@Bruce227:您能否描述一下您要完成的工作?是否要分析节点,检查节点的 DocumentVersionId 是否在 IDs 列表中,然后将该节点移动到链表的最前面?
-
我想将“移动”中的节点移动到链表中的不同位置。唯一的问题是如果我将它移到列表的前面。在这种情况下,它不会执行 foreach 将项目放回列表中。
标签: c# linked-list