-1

I have the next code:

static LinkedList<Entry> items = new LinkedList<Entry>();
public static Entry runner;

Entry is a struct that stores information from a database row.

With the next function I add Entry items to the list:

public void Prepare()
{
    // Makes connection to DB
    while(dbReader.Read())
    {
        Entry entry = new Entry(item1, item n);
        items.AddLast(entry);
    }
    runner = items.First.Value;
}

Then I call a function that correctly sets the values of that runner to the graphic elements on a window.

With two buttons I am able to move forward and backwards on that list while updating all the graphic items correctly, except in the next cases:

  1. When trying to go to the previous node while being at the first node.
  2. When trying to go to the next node while being at the last one.

How would I solve this? I want to make it behave as a circular doubly linked list.
I know that System.Collections.Generic´s LinkedList works as a single/normal/classic doubly linked list.

This is the code of the buttons:

private void previousButton()
{
    runner = items.Find(runner).Previous.Value;
    // Call function to set items<node> to graphical elements
}

nextButton() works exactly the same but uses Next instead of Previous.

I already tried to do runner = items.Find(runner).Previous.Value ?? items.Last.Value; as suggested here, but it says that ?? operator cannot be asigned to types 'Entry' & 'Entry'.

On Prepare() changing runner = items.First.Value; to just runner = items.First(); makes no difference.

jotaro-gogogo
  • 59
  • 1
  • 8
  • 2
    Sounds like you need to define your own class that works the way you want and use it instead of `LinkedList`. It's not for us to teach you how to do that or do it for you. Put some thought into the problem, do what you can and come back and ask a question if and when you encounter a specific problem. Keep in mind that you can look at the .NET source code if you want to, so you can see how the `LinkedList` class works and make appropriate changes in your own class. You may even be able to inherit `LinkedList`, although I wouldn't count on it. – user18387401 Jul 31 '22 at 04:00
  • @user18387401 I know how to make my own class that behaves as expected, I just want to know how to do it while using Microsoft's libraries. – jotaro-gogogo Jul 31 '22 at 04:03
  • 1
    `runner = items.Find(runner).Previous?.Value ?? items.Last.Value;` – Charlieface Jul 31 '22 at 04:12
  • @Charlieface that did the trick, thank you so much. – jotaro-gogogo Jul 31 '22 at 04:48

2 Answers2

1

Make Entry into a class instead of a struct and you will be able to use the null coalesce operator on it as you expect. Since you are storing references to Entry instances in a LinkedList, you'll likely get better performance from a class than a struct anyways (unless you have a strong need for struct semantics).

keithwill
  • 1,964
  • 1
  • 17
  • 26
0

I'm not sure why you don't use the exact code provided in the answer at Creating a circularly linked list in C#?

Be that as it may, you can fix your immediate problem by adding a ? null-conditional operator. This is because Previous might be null, and that would be the basis of coalescing to null using ??.

runner = items.Find(runner).Previous?.Value ?? items.Last.Value;
Charlieface
  • 52,284
  • 6
  • 19
  • 43