Linked List

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.

images

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…..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s