在链接列表C#.NET中移动项目 [英] Moving items in linked list C#.NET

查看:34
本文介绍了在链接列表C#.NET中移动项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试移动列表中的项目,但是当我与最后一个选项进行比较时,我退出了,然后才移动了移动链接列表中的项目.有没有一种方法可以在将节点放到最后并不能循环通过以移动项目之前完成该操作?

I'm trying to move items in my list but when I compare against the last option I exit out before I move the items in my move linked list. Is there a way to do that before the node gets put at the end and can't loop through to move the items?

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();
    }
}

推荐答案

这对我有用.我尝试了不同的方法,并想寻求帮助,但这不仅对我有帮助,而且对列表也很重要:

Here is what worked for me. I tried different thing and thinks for the help but here is what worked for me more than just moving to the front but also just moving through the list:

while (next != null)
{
   node = next;
   next = next.Previous;

   if (IDs.Contains(Id))
   {
      move.Add(node);
      list.Remove(node);
   }
   else
   {
      foreach (var item in move)
      {
         list.AddBefore(node, item);
         node = node.Previous;
      }
      move.Clear(); 
   }

   if (next == null) 
   {
      foreach (var item in move)
      {
         list.AddFirst(item);
      }
      move.Clear();
   }
}

这篇关于在链接列表C#.NET中移动项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆