📜  C#|获取LinkedList中包含的节点数<T>

📅  最后修改于: 2021-05-30 00:04:25             🧑  作者: Mango

LinkedList < T > .Count属性用于获取LinkedList 中实际包含的节点数。

句法:

public int Count { get; }

返回值: LinkedList中实际包含的节点数。

注意:检索此属性的值是O(1)操作。

下面给出了一些示例,以更好地理解实现:

范例1:

// C# code to get the number
// of nodes actually contained
// in 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 myList = new LinkedList();
  
        // Adding nodes in LinkedList
        myList.AddLast("Geeks");
        myList.AddLast("for");
        myList.AddLast("Data Structures");
        myList.AddLast("Noida");
  
        // To get the number of nodes actually
        // contained in the LinkedList
        if (myList.Count > 0)
            Console.WriteLine(myList.Count);
        else
            Console.WriteLine("LinkedList is empty");
    }
}

输出:

4

范例2:

// C# code to get the number
// of nodes actually contained
// in 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 myList = new LinkedList();
  
        // To get the number of nodes actually
        // contained in the LinkedList
        if (myList.Count > 0)
            Console.WriteLine(myList.Count);
        else
            Console.WriteLine("LinkedList is empty");
    }
}

输出 :

LinkedList is empty

参考:

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