tags:

views:

1651

answers:

3

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

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.

Steve Gilham
sorry about that I removed the first foreach of the move but it got added back in. I was attempting to call if it was going to end the loop and move the items. I will give that a try and see if it works. Thanks
Bruce227
Well that doesn't work just because it doesn't get to that portion with the current code and isn't called. the problem is that next is null because it is at the beginning of the list. so even if i do call the foreach node won't be going any further with .previous
Bruce227
1) My refactoring was pointless, as it's just second-guessing what LinkedList is doing for you (too many memories of by-hand 'C', I guess)2) instead of `while`, using `do {} while (node != list.First);` would give the extra turn around the loop. Alternatively, follow the loop by an unconditional foreach/addFirst.
Steve Gilham
A: 

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

Zyphrax
well the items are moved up the list. it is just if i'm moving to first there was a problem. i will try your code out. Thanks
Bruce227
nope got a infinite loop with that code.
Bruce227
@Bruce227: I've added a simple integer example to show how the code in my answer works
Zyphrax
For some reason I was when I used that code but not just looking to move items to the front. If moving up the list I was having a problem when it got to the first ones it was lost. I think there is a problem with moving multiple spaced out as well.
Bruce227
A: 

Hi,

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