C# |在链表开始处添加新的节点或值

原文:https://www . geeksforgeeks . org/c-sharp-在链接开始时添加新节点或值:/

链接列表< T >。AddFirst 方法用于在链表< T >的开始处添加一个新的节点或值。此方法的重载列表中有 2 种方法,如下所示:

  • AddFirst(链接列表 *AddFirst(T)

    add first(LinkedIn node<tT2】

    该方法用于在链表< T >开始处添加指定的新节点。

    语法:

    ```cs public void AddFirst (System.Collections.Generic.LinkedListNode node);

    ```

    在这里, 节点 是新的 LinkedListNode < T >添加在 LinkedList 的开始< T >。

    异常:

    • ArgumentNullException : 如果节点为空。
    • 无效操作异常:如果节点属于另一个链接列表< T >。

    示例:

    ```cs // C# code to add new node // at the start of LinkedList using System; using System.Collections; using System.Collections.Generic;

    class GFG {

    // Driver code     public static void Main()     {         // Creating a LinkedList of Integers         LinkedList myList = new LinkedList();

    // Adding nodes in LinkedList         myList.AddLast(2);         myList.AddLast(4);         myList.AddLast(6);         myList.AddLast(6);         myList.AddLast(6);         myList.AddLast(8);

    // To get the count of nodes in LinkedList         // before removing all the nodes         Console.WriteLine("Total nodes in myList are : " + myList.Count);

    // Displaying the nodes in LinkedList         foreach(int i in myList)         {             Console.WriteLine(i);         }

    // Adding new node at the start of LinkedList         // This will give error as node is null         myList.AddFirst(5);

    // To get the count of nodes in LinkedList         // after removing all the nodes         Console.WriteLine("Total nodes in myList are : " + myList.Count);

    // Displaying the nodes in LinkedList         foreach(int i in myList)         {             Console.WriteLine(i);         }     } } ```

    输出:

    myList 中的节点总数为:6 2 4 6 6 6 8

    未处理异常: 系统。ArgumentNullException:值不能为空。 参数名称:节点

    注:

    • 链接列表< T >接受 null 作为引用类型的有效值,并允许重复值。
    • 如果链接列表< T >为空,新节点将成为第一个最后一个
    • 这个方法是一个 O(1)运算。

    添加第一种方法

    该方法用于在链表<TT2 开始处添加一个包含指定值的新节点。

    语法:

    ```cs public System.Collections.Generic.LinkedListNode AddFirst (T value);

    ```

    这里, 是链接列表< T >开始时要添加的值。

    返回值:包含值的新链接列表节点< T >。

    示例:

    ```cs // C# code to add new node containing // the specified value at the start // of LinkedList using System; using System.Collections; using System.Collections.Generic;

    class GFG {

    // Driver code     public static void Main()     {         // Creating a LinkedList of Integers         LinkedList myList = new LinkedList();

    // Adding nodes in LinkedList         myList.AddLast(2);         myList.AddLast(4);         myList.AddLast(6);         myList.AddLast(6);         myList.AddLast(6);         myList.AddLast(8);

    // To get the count of nodes in LinkedList         // before removing all the nodes         Console.WriteLine("Total nodes in myList are : " + myList.Count);

    // Displaying the nodes in LinkedList         foreach(int i in myList)         {             Console.WriteLine(i);         }

    // Adding new node containing the         // specified value at the start of LinkedList         myList.AddFirst(20);

    // To get the count of nodes in LinkedList         // after removing all the nodes         Console.WriteLine("Total nodes in myList are : " + myList.Count);

    // Displaying the nodes in LinkedList         foreach(int i in myList)         {             Console.WriteLine(i);         }     } } ```

    输出:

    ```cs Total nodes in myList are : 6 2 4 6 6 6 8 Total nodes in myList are : 7 20 2 4 6 6 6 8

    ```

    注:

    • 链接列表< T >接受 null 作为引用类型的有效值,并允许重复值。
    • 如果链接列表< T >为空,新节点将成为第一个最后一个
    • 这个方法是一个 O(1)运算。

    参考: