C# | Obtenga el primer Node de LinkedList<T>

La propiedad LinkedList< T >.First se usa para obtener el primer Node de LinkedList<T>.

Sintaxis:

public System.Collections.Generic.LinkedListNode First { get; }

Valor devuelto: el primer LinkedListNode< T > de LinkedList< T > .

A continuación se dan algunos ejemplos para entender la implementación de una mejor manera:

Ejemplo 1:

// C# code to get the first
// node of the LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Creating a LinkedList of Strings
        LinkedList<String> myList = new LinkedList<String>();
  
        // Adding nodes in LinkedList
        myList.AddLast("Geeks");
        myList.AddLast("for");
        myList.AddLast("Data Structures");
        myList.AddLast("Noida");
  
        // To get the first node of the LinkedList
        if (myList.Count > 0)
            Console.WriteLine(myList.First.Value);
        else
            Console.WriteLine("LinkedList is empty");
    }
}

Producción:

Geeks

Ejemplo 2:

// C# code to get the first
// node of the LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Creating a LinkedList of Integers
        LinkedList<int> myList = new LinkedList<int>();
  
        // To get the first node of the LinkedList
        if (myList.Count > 0)
            Console.WriteLine(myList.First.Value);
        else
            Console.WriteLine("LinkedList is empty");
    }
}

Producción:

LinkedList is empty

Nota:

  • LinkedList acepta nulo como un valor válido para los tipos de referencia y permite valores duplicados.
  • Si LinkedList está vacío, las propiedades First y Last contienen null .
  • Recuperar el valor de esta propiedad es una operación O(1) .

Referencia:

Publicación traducida automáticamente

Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *