Moving items in linked list C#.NET

妖精的绣舞 提交于 2021-02-10 05:32:27

问题


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

回答1:


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



回答2:


Your code is interleaving the two lists -- this doesn't look right to me.
I think that instead of the repeated block

foreach (var item in move)
{
    list.AddBefore(node, item);
    node = node.Previous;
}
move.Clear();

you probably want something like

    var before = node.Previous;
    var LinkedListNode<BD> current = null;
    foreach (var item in move)
    {
        list.AddBefore(node, item);
        current = node = item;
    }
    current.Previous = before; // assumes move was not empty
    move.Clear();

to keep track of where you're inserting.




回答3:


Something like this? (I tried to base it on your code):

LinkedList<BD> list = new LinkedList<BD>(b[arg].Values);
LinkedListNode<BD> node = list.Last;
LinkedListNode<BD> terminator = null;

while (node != null && node != terminator) {
    if (IDs.Contains(node.Value.DocumentVersionId)) {
        LinkedListNode<BD> tempNode = node;
        node = node.Previous;

        list.Remove(tempNode);
        list.AddFirst(tempNode);
        if (terminator == null) terminator = tempNode;
    } else {
        node = node.Previous;
    }
}

This piece of code should move your "DocumentVersionId-matched" nodes to the front of the linked list.

Below is an example with simple integers to demonstrate how it works:

List<int> specials = new List<int> { 1, 4, 5, 7 };
List<int> source = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
LinkedList<int> list = new LinkedList<int>(source);

LinkedListNode<int> node = list.Last;
LinkedListNode<int> terminator = null;

while (node != null && node != terminator) {
    if (specials.Contains(node.Value)) {
        LinkedListNode<int> tempNode = node;
        node = node.Previous;

        list.Remove(tempNode);
        list.AddFirst(tempNode);
        if (terminator == null) terminator = tempNode;
    } else {
        node = node.Previous;
    }
}

The result linked list will contain:
1, 4, 5, 7 (the specials at the beginning of the linked list), 2, 3, 6, 8

An endless loop should be impossible.

Answer edits:
- node = list.First to node = list.Last
- added an example with integers



来源:https://stackoverflow.com/questions/1203270/moving-items-in-linked-list-c-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!