
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Get the number of nodes contained in LinkedList in C#
To get the number of nodes contained in the LinkedList, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main() { LinkedList<String> list = new LinkedList<String>(); list.AddLast("A"); list.AddLast("B"); list.AddLast("C"); list.AddLast("D"); list.AddLast("E"); list.AddLast("F"); list.AddLast("G"); list.AddLast("H"); list.AddLast("I"); list.AddLast("J"); Console.WriteLine("Count of nodes = " + list.Count); Console.WriteLine("First Node = "+list.First.Value); list.Clear(); Console.WriteLine("Count of nodes (updated) = " + list.Count); } }
Output
This will produce the following output −
Count of nodes = 10 First Node = A Count of nodes (updated) = 0
Example
Let us see another example −
using System; using System.Collections.Generic; public class Demo { public static void Main(String[] args) { LinkedList<String> list1 = new LinkedList<String>(); list1.AddLast("One"); list1.AddLast("Two"); list1.AddLast("Three"); list1.AddLast("Four"); list1.AddLast("Five"); Console.WriteLine("Elements in LinkedList1..."); foreach (string res in list1) { Console.WriteLine(res); } LinkedList<String> list2 = new LinkedList<String>(); list2.AddLast("India"); list2.AddLast("US"); list2.AddLast("UK"); list2.AddLast("Canada"); list2.AddLast("Poland"); list2.AddLast("Netherlands"); Console.WriteLine("Elements in LinkedList2..."); foreach (string res in list2) { Console.WriteLine(res); } LinkedList<String> list3 = new LinkedList<String>(); Console.WriteLine("Count of nodes in LinkedList3 = "+list3.Count); list3 = list2; Console.WriteLine("Count of nodes in LinkedList3 (Updated) = "+list2.Count); Console.WriteLine("Is LinkedList3 equal to LinkedList2? = "+list3.Equals(list2)); } }
Output
This will produce the following output −
Elements in LinkedList1... One Two Three Four Five Elements in LinkedList2... India US UK Canada Poland Netherlands Count of nodes in LinkedList3 = 0 Count of nodes in LinkedList3 (Updated) = 6 Is LinkedList3 equal to LinkedList2? = True
- Related Articles
- Get the number of elements contained in Collection in C#
- Get the number of elements contained in SortedList in C#
- Get the number of elements contained in the Queue in C#
- Get the number of elements contained in the Stack in C#
- Get the number of elements actually contained in the ArrayList in C#
- Get the number of key/values pairs contained in OrderedDictionary in C#
- Get the number of key/value pairs contained in ListDictionary in C#
- Removing all nodes from LinkedList in C#
- Get the first node of the LinkedList in C#
- Get the last node of the LinkedList in C#
- Number of elements contained in the BitArray in C#?
- Get SubList from LinkedList in Java
- Java Program to Get the middle element of LinkedList in a single iteration
- Golang Program to Get the middle element of LinkedList in a single iteration
- How to find the largest number contained in a JavaScript array?

Advertisements