Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Singly LinkedList Traversal using C#
A LinkedList in C# is a doubly-linked list that allows efficient insertion and removal of elements. Traversal refers to visiting each node in the LinkedList sequentially to access or process the data.
The LinkedList<T> class in C# provides various methods to traverse through its elements, with the most common approach being the foreach loop.
Syntax
Following is the syntax for declaring a LinkedList −
var linkedList = new LinkedList<DataType>();
Following is the syntax for traversing a LinkedList using foreach −
foreach(var item in linkedList) {
// Process each item
}
Using foreach Loop for Traversal
The simplest and most commonly used method to traverse a LinkedList is using a foreach loop, which automatically handles the iteration −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(string[] args) {
var a = new LinkedList<string>();
a.AddLast("Tim");
a.AddLast("Tom");
a.AddLast("John");
a.AddLast("Alice");
Console.WriteLine("Traversing LinkedList:");
foreach(var res in a) {
Console.WriteLine(res);
}
}
}
The output of the above code is −
Traversing LinkedList: Tim Tom John Alice
Using LinkedListNode for Manual Traversal
You can also traverse a LinkedList manually using LinkedListNode<T> objects, which provides more control over the traversal process −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(string[] args) {
var numbers = new LinkedList<int>();
numbers.AddLast(10);
numbers.AddLast(20);
numbers.AddLast(30);
numbers.AddLast(40);
Console.WriteLine("Manual traversal using nodes:");
LinkedListNode<int> current = numbers.First;
while(current != null) {
Console.WriteLine("Value: " + current.Value);
current = current.Next;
}
}
}
The output of the above code is −
Manual traversal using nodes: Value: 10 Value: 20 Value: 30 Value: 40
Reverse Traversal
Since LinkedList in C# is doubly-linked, you can traverse it in reverse order starting from the last node −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(string[] args) {
var fruits = new LinkedList<string>();
fruits.AddLast("Apple");
fruits.AddLast("Banana");
fruits.AddLast("Orange");
fruits.AddLast("Mango");
Console.WriteLine("Forward traversal:");
foreach(var fruit in fruits) {
Console.WriteLine(fruit);
}
Console.WriteLine("\nReverse traversal:");
LinkedListNode<string> current = fruits.Last;
while(current != null) {
Console.WriteLine(current.Value);
current = current.Previous;
}
}
}
The output of the above code is −
Forward traversal: Apple Banana Orange Mango Reverse traversal: Mango Orange Banana Apple
Conclusion
LinkedList traversal in C# can be accomplished using foreach loops for simple iteration or manually using LinkedListNode<T> objects for more control. The doubly-linked nature allows both forward and reverse traversal efficiently.
