C# | Get the number of nodes contained in LinkedList<T>
LinkedList<T>.Count property is used to get the number of nodes actually contained in the LinkedList<T>.
Syntax:
public int Count { get; }
Return Value: The number of nodes actually contained in the LinkedList.
Note: Retrieving the value of this property is an O(1) operation.
Below given are some examples to understand the implementation in a better way:
Example 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<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 number of nodes actually // contained in the LinkedList if (myList.Count > 0) Console.WriteLine(myList.Count); else Console.WriteLine( "LinkedList is empty" ); } } |
chevron_right
filter_none
Output:
4
Example 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< int > myList = new LinkedList< int >(); // 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" ); } } |
chevron_right
filter_none
Output :
LinkedList is empty
Reference:
Recommended Posts:
- C# | Get the number of key/value pairs contained in ListDictionary
- C# | Get the number of elements contained in the Stack
- C# | Get the number of elements contained in the Queue
- C# | Number of elements contained in the BitArray
- C# | Get the number of elements contained in Collection<T>
- C# | Get the number of elements actually contained in the ArrayList
- C# | Get the number of elements contained in SortedList
- C# | Get the number of key/values pairs contained in OrderedDictionary
- C# | Check whether an element is contained in the ArrayList
- C# | Removing all nodes from LinkedList<T>
- C# | Get the number of strings in StringCollection
- C# | Get or set the number of elements in the BitArray
- C# | Get the number of key/value pairs in the StringDictionary
- C# | Get or set the number of elements that the ArrayList can contain
- C# | Get the number of elements in the SortedSet
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.