1

I need to create an Array with Linked list capacities.

Basically, I need a static index based list (like array), but with the possibility to get next and previous field (and easily loop back and forward through list, like with linked list). Note: Array is 2 dimensional. I use a custom class as array values. So I can set previous and next property for each instance.

Is there a built in C# collection for this? If not, any suggestions on how to create a very simple version of this? (I already have a version of this, consisting of 2 methods. One that loops forward to set the previous field, and one to loop backwards that set the next field, but it's still to messy).

Thanks in advance

EDIT:

The problem is my use of 2dimensional array. If loop through my array:

            for (byte x = 0; x < Grid.GetLength(0); x++) 
            {
                for (byte y = 0; y < Grid.GetLength(1); y++) /
                {
                    //At certain point, I need to get the previous field. I can do:
                    if (y != 0)
                    {
                        y -= 2; //-2 because I will y++ in for. Already getting messy
                    }
                    else 
                    {
//What if y == 0? Then I can't do y--. I should get max y and  do x-- to get previous element:

                        y = (byte)(Grid.GetLength(1) - 1); //to get max value y

                        x--;
                    }
}
    }

2 Answers 2

4

There is a built-in LinkedList<T> class.

But from your description why wouldn't an array work? It's static, and index-based, and you can easily get the next and previous element by incrementing / decrementing the index. It's hard to see exactly what you need from your code, but I'd like to point out that you can easily enumerate over a multi-dimensional array with:

var arry = new int[2,3];
foreach(var item in arry)
{
    ...
}

So you might be able to combine this with a Stack<T> structure (push items on the stack and pop them off to get the previous).

Alternatively, you can turn the array into a LinkedList directly.

var list = new LinkedList(arry.Cast<int>()); // flattens array

Or to preserve the indexes from the original array and still loop through the values as a linked list use:

var list = new LinkedList(arry.Cast<int>.Select((item, i) => new 
{ 
    Item = item, 
    Index1 = i % arry.GetLength(1), 
    Index2 = i / arry.GetLength(0) 
}));
var node = list.First;
while(node.Next != null)
{
    Console.WriteLine("Value @ {1}, {2}: {0}", node.Value.Item, node.Value.Index1, node.Value.Index2);
    // on some condition move to previous node
    if (...)
    {
        node = node.Previous;
    }
    else
    {
        node = node.Next;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Because I'm using a 2dimensional array. I will place code snippets and explain the problem in my answer rather then here. Ty :)
Thanks. It looks like I'm very bad at explaining :( As explained in the code (or at least tryed), I need to loop back and forward dynamically through the list, which I'm not able to do eassily (see comments in code) with a 2D array
@user1933169 Okay, I've updated my answer. I think I've provided a solution that suits your needs.
2

No, you don't. Instead of abandoning traditional arrays in lieu of "smart linked node arrays" which is what it seems like you're heading towards, try just adding a couple variables in your loop body:

byte x_len = Grid.GetLength(0);
byte y_len = Grid.GetLength(1);
byte prev_x, next_x, prev_y, next_y;

for (byte x = 0; x < x_len; ++x) 
{
  prev_x = x == 0? x_len - 1 : x - 1;
  next_x = x == x_len - 1? 0 : x + 1;
  for (byte y = 0; y < y_len; ++y)
  {
    prev_y = y == 0? y_len - 1 : y - 1;
    next_y = y == y_len - 1? 0 : y + 1;

    // here, you have access to the next and previous
    // in both directions, satisfying your requirements
    // without confusing your loop variables.

  }
}

3 Comments

This is probably a good answer. But I'm far from familiar with the syntax your using.
@user1933169 ?: is the conditional or ternary operator.
k, will look into it. Tyvm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.