📜  C#|在LinkedList的开头添加新节点或值<T>

📅  最后修改于: 2021-05-29 17:03:30             🧑  作者: Mango

LinkedList .AddFirst方法用于在LinkedList 的开始处添加新的节点或值。此方法的重载列表中有2种方法,如下所示:

  1. AddFirst(LinkedList )
  2. AddFirst(T)

AddFirst(LinkedListNode < T >)

此方法用于在LinkedList >的开头添加指定的新节点。

句法:

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

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

例外情况:

  • ArgumentNullException:如果节点为null。
  • InvalidOperationException:如果该节点属于另一个LinkedList < T >。

例子:

// 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);
        }
    }
}

输出:

笔记:

  • LinkedList < T >接受null作为引用类型的有效值,并允许重复值。
  • 如果LinkedList < T >为空,则新节点将成为FirstLast
  • 此方法是O(1)操作。

AddFirst(T)方法

此方法用于在LinkedList >的开头添加包含指定值的新节点。

句法:

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

此处,是要在LinkedList < T >的开头添加的值。

返回值:包含值的新LinkedListNode < T>。

例子:

// 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);
        }
    }
}

输出:

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

笔记:

  • LinkedList < T >接受null作为引用类型的有效值,并允许重复值。
  • 如果LinkedList < T >为空,则新节点将成为FirstLast
  • 此方法是O(1)操作。

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.linkedlist-1.addfirst?view=netframework-4.7.2