It was necessary to implement a ring list. Made through extension methods. But there is one nuance. How to implement the return from the method of the first value in the node? In this case, I always return the second value ("Tanya") at the first pass, then after the last node, the transition to the first value.

class Program { static void Main(string[] args) { // Π‘ΠΎΠ·Π΄Π°Π΄ΠΈΠΌ связный список LinkedList<string> linkedList = new LinkedList<string>(); // Π”ΠΎΠ±Π°Π²ΠΈΠΌ нСсколько элСмСнтов linkedList.AddFirst("Вася"); linkedList.AddFirst("ΠŸΠ΅Ρ‚Ρ"); linkedList.AddFirst("Ваня"); linkedList.AddFirst("Коля"); // ΠžΡ‚ΠΎΠ±Ρ€Π°Π·ΠΈΠΌ элСмСнты Π² прямом Π½Π°ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠΈ LinkedListNode<string> node; Console.WriteLine("Π­Π»Π΅ΠΌΠ΅Π½Ρ‚Ρ‹ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ Π² прямом Π½Π°ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠΈ: "); for (node = linkedList.First; node != null; node = node.Next) Console.Write(node.Value + "\t"); Console.WriteLine(); var result = GetValue(linkedList.First); Console.WriteLine($"+++++++ {result} ++++++++"); Console.ReadKey(); } private static string GetValue(LinkedListNode<string> node) { node = CircularLinkedList.NextOrFirst(node); return node.Value; } } 

Extension method for a single-linked list:

  public static class CircularLinkedList { public static LinkedListNode<T> NextOrFirst<T>(this LinkedListNode<T> current) { return current.Next ?? current.List.First; } public static LinkedListNode<T> PreviousOrLast<T>(this LinkedListNode<T> current) { return current.Previous ?? current.List.Last; } } 

enter image description here

  • you yourself wrote CircularLinkedList.NextOrFirst(node); , so Kolya has Next, which is Tanya, because you see Tanya instead of Kolya - tym32167
  • Yes, I understand that and I did it to get the next node, I need to make some condition that I first return First from the method, and only then Next. - JDo

2 answers 2

The GetValue method has an incorrect name:

 private static string GetNextCircularValue(LinkedListNode<string> node) { node = CircularLinkedList.NextOrFirst(node); return node.Value; } 

Perhaps you wanted to do the following:

 private static string GetValueAndAdvance(ref LinkedListNode<string> node) { string result = node.Value; node = CircularLinkedList.NextOrFirst(node); return result; } node = linkedList.First; var result = GetValueAndAdvance(ref node); 
  • Your answer is absolutely not relevant to the question. And in no way solves it. - JDo
  • 2
    @JDo very much applies. You were told that you wrote the code. And what code did you need? - 4per
  • one
    @JDo To get insight into the essence of my answers over them should be meditated). - Igor
  • Thank you string GetValueAndAdvance(ref LinkedListNode<string> node); It works perfectly at this stage! - JDo 2:41 pm

Since in this code

 private static string GetValue(LinkedListNode<string> node) { node = CircularLinkedList.NextOrFirst(node); return node.Value; } 

first the next element is obtained, and only then the value is taken; when the first element is transmitted, the value will be obtained from the next one.

To solve, it is enough to transmit not the first element, but the last one:

 var result = GetValue(linkedList.Last); 
  • Thank! This is also a good decision !!! Immediately did not see yesterday :) - JDo
  • @JDo, instead of getting the value, it was better to get the next item, and get the value through the usual Value property - Grundy
  • one
    @JDo, well, I already did extensions, it was worth using it as: node = node.NextOrFirst(); - Grundy
  • Is it through Linq? - JDo
  • @JDo, and here linq? - Grundy