Introduction
In computer science, a linked list is a data structure consisting of a group of nodes which together represent a sequence.
Node
The Node full fills two function. The first function is a mechanism of provide a piece of data to the node.For example integer value 1 0 in the below figure.
and the second function is to pointing to the next node.
How to create a Linked List using C#?
public class LinkedList
{
public static void ConstructLinkedList()
{
//Creating a first node and assign the value 1 to the firstnode
var firstNode = new Node {CurrentValue = 1};
var secondNode = new Node {CurrentValue = 2};
//Pointing the FirstNode to the SecondNode
firstNode.NextNode =secondNode;
//Creating the third node and assign a value to 3
var thirdNode = new Node {CurrentValue = 3};
//Pointing the second node to the third node
secondNode.NextNode = thirdNode;
//Display all the Linked List
DisplayLinkedList(firstNode);
}
public static void DisplayLinkedList(Node node)
{
//Looping through all the nodes and print the value in it
while (node!=null)
{
Console.WriteLine(node.CurrentValue);
node = node.NextNode;
}
}
}
// This is a node which contain two properties , One for setting the current node and other is for setting the next node
public class Node
{
public int CurrentValue { get; set; }
public Node NextNode { get; set; }
}
Enjoy coding…..